【问题标题】:Insert a nested object in Firebase在 Firebase 中插入嵌套对象
【发布时间】:2017-10-17 17:15:47
【问题描述】:

被这个问题困扰了一段时间,所以我想做的是: 当我创建表单时,将表单连同用户列表以及他们的电子邮件地址一起写入数据库 - 如下所示:

formsById -> formId -> userId(包含用户电子邮件地址)

然后我可以像这样过滤当前用户的所有表单:

  getFormList() {
    return firebase.database().ref('/formsByID').orderByChild('userList').startAt(this.userId).endAt(this.userId);
  }

这是我目前写入数据库的方式:

writeNewForm(formName: string, formDate: string): Promise<any> {

    // A post entry.
    let postData = {
      name: formName,
      date: formDate,
      userList: this.userId // I want to nest the email in here
    };

    // Get a key for a new Post.
    let newPostKey:string = this.formListRef.push().key;

    // Write the new post's data simultaneously in the posts list and the user's post list.
    let updates = {};
    // write to the users record
    updates[this.userPath + '/' + newPostKey] = true; // just set this to true be
    updates[this.formsByIdPath + '/' + newPostKey] = postData;

    return this.rootRef.update(updates);
    //return rootRef.update(updates);

  }

但我不知道如何将我的用户电子邮件地址嵌套在他们的 ID 下。

任何帮助表示赞赏。

【问题讨论】:

    标签: angular ionic-framework firebase-realtime-database


    【解决方案1】:

    我认为您必须将您的帖子条目修改为:

    // A post entry.
    let postData = {
      name: formName,
      date: formDate,
      userList: [
        {email: this.userId} // I want to nest the email in here
      ]
    };
    

    或创建一个值数组:

    // A post entry.
    let postData = {
      name: formName,
      date: formDate,
      userList: [
        this.userId // I want to nest the email in here
      ]
    };
    

    然后您可以进行深度路径更新。

    您可以在这里找到更多信息: https://firebase.googleblog.com/2015/09/introducing-multi-location-updates-and_86.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-19
      • 2016-01-03
      • 1970-01-01
      相关资源
      最近更新 更多