![](/img/trans.png)
[英]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.