简体   繁体   English

如何将数据写入特定位置Firebase Web

[英]How to write data into a specific location Firebase Web

I want to write data into a specific location in the database. 我想将数据写入数据库中的特定位置。 Let's say, I have a couple of users in the database. 假设我在数据库中有几个用户。 Each of them has their own personal information, including their e-mails. 他们每个人都有自己的个人信息,包括电子邮件。 I want to find the user based on the e-mail, that's to say by using his e-mail (but I don't know exactly whose e-mail it is, but whoever it is do something with that user's information). 我想根据电子邮件来查找用户,也就是说使用他的电子邮件(但是我不知道确切是谁的电子邮件,但是无论是谁处理的都是该用户的信息)。 To be more visible, here is my database sample. 为了更加明显,这是我的数据库示例。

在此处输入图片说明

Now, while working on one of my javascript files, when the user let's say name1 changes his name, I update my object in javascript and want to replace the whole object under ID "-LEp2F2fSDUt94SRU0cx". 现在,在处理我的一个javascript文件时,当用户说name1更改其名称时,我在javascript中更新了我的对象,并希望替换ID为“ -LEp2F2fSDUt94SRU0cx”的整个对象。 To cut short, I want to write this updated object in the path ("Users/-LEp2F2fSDUt94SRU0cx") without doing it by hand and just "knowing" the e-mail. 简而言之,我想将此更新的对象写在路径(“ Users / -LEp2F2fSDUt94SRU0cx”)中,而无需手工完成,而只是“知道”电子邮件。 So the logic is "Go find the user with the e-mail "name1@yahoo.com" and replace the whole object with his new updated object". 因此逻辑是“去用电子邮件“ name1@yahoo.com”查找用户,然后用新的更新对象替换整个对象”。 I tried to use orderByChild("Email").equalTo("name1@yahoo.com").set(updated_object), but this syntax does not work I guess. 我尝试使用orderByChild(“ Email”)。equalTo(“ name1@yahoo.com”)。set(updated_object),但是我猜这种语法不起作用。 Hopefully I could explain myself. 希望我能解释自己。

The first part is the query, that is separate from the post to update. 第一部分是查询,与要更新的帖子分开。 This part is the query to get the value: 这部分是获取值的查询:

ref.child('users').orderByChild("Email").equalTo("name1@yahoo.com")

To update, you need to do something like this once you have the user id from the query result: 要进行更新,一旦从查询结果中获得了用户ID,就需要执行以下操作:

ref.child('users').child(userId).child("Email").update(newValue);

firebase.database.Query firebase.database.Query

A Query sorts and filters the data at a Database location so only a subset of the child data is included. 查询对数据库位置的数据进行排序和过滤,因此仅包含子数据的一个子集。 This can be used to order a collection of data by some attribute (for example, height of dinosaurs) as well as to restrict a large list of items (for example, chat messages) down to a number suitable for synchronizing to the client. 这可用于通过某些属性(例如,恐龙的高度)对数据集合进行排序,以及将大量项目(例如,聊天消息)限制为适合与客户端同步的数量。 Queries are created by chaining together one or more of the filter methods defined here. 通过将此处定义的一个或多个过滤方法链接在一起来创建查询。

 // Find all dinosaurs whose height is exactly 25 meters. var ref = firebase.database().ref("dinosaurs"); ref.orderByChild("height").equalTo(25).on("child_added", function(snapshot) { console.log(snapshot.key); }); 

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

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