在Firebase中插入嵌套对象

时间:2017-10-17 07:09:56

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

暂时停留在这个问题上,所以我想做的是: 当我创建表单时,将表单连同用户列表及其电子邮件地址一起写入数据库 - 如下所示:

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下嵌套我的用户电子邮件地址。

任何帮助表示感谢。

1 个答案:

答案 0 :(得分: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