【问题标题】:Reading data after writing to it in Firebase在 Firebase 中写入数据后读取数据
【发布时间】:2019-01-03 23:19:44
【问题描述】:

在我的实时数据库中创建项目后,我试图导航到特定 ID。

文档提供了一个解决方案,但它没有按所示工作。

在我的 db ref 上使用 push().set() 方法后,我尝试创建一个回调函数,该函数将获取使用 push.().set() 方法创建的特定 ID,然后使用该 ID 导航。

db.ref.push().set({
    ...
}).then(function(){
    router.navigate(ref/ + db.ref.key /)
})

在该 ref 点创建新对象后,我想使用 firebase 创建的 ID 导航到使用该 ID 的特定路由。

【问题讨论】:

  • 那么,您使用什么库进行路由?
  • @joelgullander 使用骨干网的路由器。问题是使用 set 后没有取回 id。

标签: javascript firebase firebase-realtime-database


【解决方案1】:

问题在于db.ref.push() 返回一个new 引用对象,该对象具有唯一的ID。您没有获得该 ID 的值。您可以重构事物,使其类似于:

let newref = db.ref.push();
newref.set({
  ...
})
.then( () => {
  // You can use "newref" here
  // The last part of the full reference path is in "key"
  let id = newref.key;

  // Do something with the id as part of your route
});

【讨论】:

  • 这完全符合我的预期。不知道为什么文档不起作用。很好的答案,也感谢您的快速回复!
【解决方案2】:

设置

set(value, onComplete) => 返回包含 void 的 firebase.Promise

所以你是绝对正确的,你应该能够通过回调看到操作已解决,但是承诺不会给你任何密钥。

但是,您应该能够链接此事件:

db.ref.push().set({
    ...
}).getKey().then(data => console.log(data))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多