【发布时间】:2021-04-01 05:20:54
【问题描述】:
我正在尝试从 firebase ref.on('value' 或 ref.once('value' 调用中了解返回值;
所以我可以在后面的.then( 中使用从那里返回的值。
我试图从
例如ref.on('value', snap => { return 'Test Text or whatever'},我总是得到一个对象,其中.node_.value_ 将是来自firebase 调用的.val()。
对此行为的解释将不胜感激(目前我正在编写不整洁且令人费解的代码来完成工作,并希望避免这种情况)。
var ref = firebase.database().ref('myAddress/etc..');
ref.once('value', snap => {
o0 = snap.val()
console.log('In the once o0:')
console.log(o0) // works perfectly
// return 'Test text' // does not return the text
// return o0 // does not return o0
}).then( in0 => {
console.log(in0)
console.log(`In the first then in0.node_.value_ is:`)
console.log(in0.node_.value_)
return in0.node_.value_
}).then( in1 => {
console.log(`In the second then input was:`)
console.log(in1) // here I begin to get what I want the value from: in0.node_.value_
return 'Some text from custom then'
}).then( in0 => {
console.log('In the third then input was:')
console.log(in0) // this prints: 'Some text from custom then', as my sanity check
})
我试图真正理解这里发生了什么,所以我编写的代码是明智的。 我可以在 `on('value...' 调用中编写功能,但这不适用于我正在开发的应用程序的其他部分。 感谢 Firebase 文档中的解释甚至链接。
【问题讨论】:
-
您没有在任何地方使用从
once返回的值。 -
如果你指的是
snap的值,我用它来获取o0然后console.logo0;问题是我可以从once电话中返回其他内容吗?我做不到,也无法理解为什么不这样做。谢谢
标签: javascript firebase firebase-realtime-database promise