[英]Getting an error when trying to write a promise: Promise resolver object is not a function
[英]Getting an error when trying to use Promise
我不确定这个问题是否太具体。 我在更新数据库的函数中使用firebase:
UpdatePeerNotification = (content) => {
firebase.database().ref('users').child(this.props.match.params.username).
.update('something)
}
如果我通常将其称为this.updatePeerNotification('abc'); 它正在工作但是尝试使用时
Promise(this.updatePeerNotification('abc')).then(() => do something))
要么
this.updatePeerNotification('abc').then(() => do something))
它会引发以下错误:未处理的拒绝(TypeError):未定义不是承诺
正常调用该函数但附加.then会出错。 另外,刷新页面后,该函数被调用而没有错误
在此先多谢!
你不返回从一个承诺(或其他任何东西) UpdatePeerNotification
,从而导致主叫方试图调用then
在undefined
(默认返回值不返回任何值的函数)。
您可能正在寻找返回update()
的结果,这是一个可以在服务器上完成更新时解决的承诺:
UpdatePeerNotification = (content) => {
return firebase.database().ref('users').child(this.props.match.params.username).
.update('something)
}
当返回类型为Promise时,您应该在方法中处理拒绝
updatePeerNotification('abc').then(() => dosomething).catch(function (error){return Promise.reject(error)}))
这应该有所帮助。
每次使用Promise时,您都必须根据自己的情况解决它或拒绝它。 处理之后,您只能执行.then。 出于调试目的,我个人使用chrome控制台查看承诺状态。 因此,我只建议您在以下功能中检查Promise的状态。
UpdatePeerNotification = (content) => { return firebase.database().ref('users').child(this.props.match.params.username). .update('something) }
var promise1 = new Promise(function(resolve,reject){ resolve(23); }); promise1.then(function(value) { console.log(value); /* expected output: 23 Promise {<resolved>: undefined} __proto__: Promise [[PromiseStatus]]: "resolved" [[PromiseValue]]: undefined */ });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.