gpt4 book ai didi

javascript - 尝试使用 Promise 时出错

转载 作者:行者123 更新时间:2023-11-30 14:18:22 25 4
gpt4 key购买 nike

我不确定这个问题是否过于具体。我在更新数据库的函数中使用 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))

它抛出以下错误:未处理的拒绝(类型错误):未定义不是 promise

为什么定期调用该函数可以正常工作但附加 .then 会出现该错误。另外,刷新页面后,调用该函数没有错误

提前致谢!

最佳答案

您没有从 UpdatePeerNotification 返回 promise (或其他任何内容),这导致调用者尝试在 undefined 上调用 then (不返回任何值的函数的默认返回值)。

您可能希望返回 update() 的结果,这是一个在服务器上完成更新时解析的 promise :

UpdatePeerNotification = (content) => {
return firebase.database().ref('users').child(this.props.match.params.username).
.update('something)
}

关于javascript - 尝试使用 Promise 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53165557/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com