简体   繁体   English

我如何将新项目添加到Firebase中的同一节点

[英]How i can add new item to the same node in Firebase

I want to add a new item to my list, I use firebase without user(email,password) 我想将新商品添加到列表中,我在没有用户的情况下使用firebase(电子邮件,密码)

I have page for search and other to show detail , how i can add more detail for the same node , And i don't know which node you want to add a child to. 我有用于搜索的页面以及其他页面以显示详细信息,如何为同一个节点添加更多详细信息,并且我不知道要将孩子添加到哪个节点。

I want to add new items like this:- 我想添加这样的新项目:-

Start with: 从...开始:

-vdjs43ndjufd87

     "name" : Jak

    "university" : Harvard

End with: 结尾为:

-vdjs43ndjufd87

     "name" : Jak

     "university" : Harvard

     "age" : 23

this code is not working with me:- 此代码不适用于我:-

 let key = ref.child("posts").childByAutoId().key

 let post = ["uid": userID, "author": username, "title": title, "body": body]

let childUpdates = ["/posts/(key)": post, "/user-posts/(userID)/(key)/": post]

ref.updateChildValues(childUpdates)

You have to know the id of post in which you want to add a new item. 您必须知道要在其中添加新项目的帖子的ID。 Otherwise there isn't any way to solve your problem. 否则,没有任何方法可以解决您的问题。 For example, you have a page with table view which shows list of posts. 例如,您有一个带有表视图的页面,该页面显示了帖子列表。 Also to populate cells of table you hold an array of posts. 另外,要填充表格的单元格,您还需要放置一系列帖子。 Every post object should have id property. 每个post对象都应具有id属性。 When you select row in table you should get indexPath of it and get post from array with indexPath.row index. 当您选择表中的行时,您应该获取它的indexPath并使用indexPath.row index从数组中获取帖子。 From selected post you get id and this is all. 从选定的帖子中,您可以获得ID,仅此而已。 Now you have post id and you can download info about this post and also add some new items to node with this id. 现在,您具有了帖子ID,可以下载有关该帖子的信息,还可以向具有此ID的节点添加一些新项目。

ref.child("posts").child(postId).updateChildValues(childUpdates)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM