我正在尝试创建一个带有封面照片的组频道,
this.sendBirdInstance.GroupChannel.createChannelWithUserIds(userIds, true, this.groupName, this.groupPhotoFile, '', function (createdChannel, error) {
...
}
根据文档,我可以添加一个URL或文件
coperl:封面图像的文件或URL,您可以将其获取到 渲染到UI。
但是,当添加文件时,我总是会得到: "SendBirdException", code: 800110, message: "Invalid arguments."
是否有一种方法可以用文件而不是URL创建组(因为我希望用户上传文件)?
谢谢,
正如您已经经历的那样(我看到您几天前在Github中创建了一个问题)Sendbird的支持有点不可靠。
他们只提供JavaScript SDK的缩小版本(我个人觉得很差)也在提供帮助。
无论如何,我可以隔离createChannelWithUserIds
函数:
! function (e, n) {
// ...
}(this, function () {
// ...
var h = function (e) {
for (var n in e) if (e.hasOwnProperty(n)) return !1;
return Array.isArray(e) ? JSON.stringify(e) === JSON.stringify([]) : JSON.stringify(e) === JSON.stringify({})
},
// ...
A = function () { // it returns SendBird function
// ...
var w = function (e) { // w is this.GroupChannel
// ...
w.createChannelWithUserIds = function () {
// ...
// here comes the param validation (I've added spaces for a better lecture):
if (!Array.isArray(e) || "boolean" != typeof n || "string" != typeof t && null !== t && void 0 !== t || "string" != typeof r && h(r) && null !== r && void 0 !== r || "string" != typeof a && null !== a && void 0 !== a || "string" != typeof i && null !== i && void 0 !== i) return void U(null, new p("Invalid arguments.", J.INVALID_PARAMETER), s);
// It will return "Invalid arguments." if any of the conditions evaluates to true
// ...
}
}
}
return function () {
// ...
}().SendBird
});
您正在使用这样的函数:
createChannelWithUserIds(o, n, t, r, a, s);
因此,第四参数(r
)是coverURL
:带封面图片的文件(this.groupPhotoFile
);
其验证基本上是在说:
"string" != typeof r // if `r` is not a string (a URL)
&& h(r) // and the returned value of function h(r) is true
&& null !== r // and it is not null
&& void 0 !== r // and it is not undefined
该参数无效。
您的文件不是字符串,而不是null,也不是未定义的,因此所有内容都归结为h()
函数:
var h = function (e) {
for (var n in e) if (e.hasOwnProperty(n)) return !1;
return Array.isArray(e) ? JSON.stringify(e) === JSON.stringify([]) : JSON.stringify(e) === JSON.stringify({})
}
上面的函数首先检查是否具有对象本身的成员的任何属性(即不是属于原型链的属性)。然后,如果它没有任何属性,它将检查Stringify对象/数组是否等于空对象/数组。
我不能说开发人员在验证文件时的意图是什么,但是标准文件对象:
- 在其原型链中具有属性,但没有直接分配给实例,因此第一个条件是
true
。 - 当Stringify在当今所有主要浏览器中返回一个空对象时(并非总是如此),因此第二个条件也是
true
。
正如我们之前看到的,我们需要h()
才能返回false
:如果验证返回true
。
要解决此行为,您可以将h()
功能更改为以下内容:
var h = function( e ){
return !(e instanceof File);
// or return e.constructor != File;
// or return Object.getPrototypeOf( e ) != File.prototype );
// or return e.__proto__ != File.prototype )
// or return e.constructor.prototype != File.prototype )
}
,但我不会惹。它可以用于不同目的的将来版本。
所以您最好的选择是将createChannelWithUserIds()
功能更改为:
- 从验证中删除对
h()
函数的调用。 - 用您自己的文件验证的调用 将其替换
为此,您可以在sendbird实例中覆盖函数:
var sb = new SendBird( { appId: ... } );
sb.GroupChannel.createChannelWithUserIds = function(){ ... };
,但不能保证工作并可能在将来的版本中打破,因此我只需编辑sendbird.min.js文件即可。换句话说,替换:
if(!Array.isArray(e)||"boolean"!=typeof n||"string"!=typeof t&&null!==t&&void 0!==t||"string"!=typeof r&&h(r)&&null!==r&&void 0!==r||"string"!=typeof a&&null!==a&&void 0!==a||"string"!=typeof i&&null!==i&&void 0!==i)return void U(null,new p("Invalid arguments.",J.INVALID_PARAMETER),s);
with:
if(!Array.isArray(e)||"boolean"!=typeof n||"string"!=typeof t&&null!==t&&void 0!==t||"string"!=typeof a&&null!==a&&void 0!==a||"string"!=typeof i&&null!==i&&void 0!==i)return void U(null,new p("Invalid arguments.",J.INVALID_PARAMETER),s);
在当前版本(v3.0.41)中,您会发现上述代码的两个巧合:一个用于createChannel
,另一个用于createChannelWithUserIds
,您可以替换两者。
当然,编辑.js文件很烦人,因为您每次升级SendGrid都需要注意更换代码。您可以在CI管道内部创建一个自动任务,以为您完成。
希望SendGrid开发人员将确认您的问题并在以后的版本中进行修复。