使用 JavaScript 在 Firebase 中查找和修改(或删除)匹配节点的确切语法
Exact syntax to find and modify (or delete) a matching node in Firebase with JavaScript
我需要遍历我的 firebase 并在 /users/{currentUser}/things/
中的 thing.feature === "something"
中找到特定记录,然后修改或删除该节点。这样做的确切语法是什么?
我查看了文档,但发现混合了一些无用的陈述,例如
When using orderByValue(), children will be ordered by their value.
或者 equalTo()
的 Return 值为
The generated Query.
Firebase documentation on querying 中后面的示例之一给出了这方面的示例。
var ref = new Firebase("https://dinosaur-facts.firebaseio.com/dinosaurs");
ref.orderByChild("height").equalTo(25).on("child_added", function(snapshot) {
console.log(snapshot.key());
});
当应用于您的问题时,它转换为:
var ref = new Firebase('https://<your-app>.firebaseio.com/user');
var things = ref.child(yourUserId).child('things');
var query = things.orderByChild('feature').equalTo('something');
query.on('child_added', function(snapshot) {
console.log('The value of the thing is now: '+JSON.stringify(snapshot.val()));
// We can remove this node with
// snapshot.ref().remove
// This will fire a child_removed event
// We can update this node with
// snapshot.ref().update({ name: 'Linda H' });
// This in turn will fire a child_changed event
});
如评论中所述,child_added
事件将针对现有子节点和在您 运行 查询之后添加的节点触发。本质上,它会为查询中的任何新内容触发。
我需要遍历我的 firebase 并在 /users/{currentUser}/things/
中的 thing.feature === "something"
中找到特定记录,然后修改或删除该节点。这样做的确切语法是什么?
我查看了文档,但发现混合了一些无用的陈述,例如
When using orderByValue(), children will be ordered by their value.
或者 equalTo()
的 Return 值为
The generated Query.
Firebase documentation on querying 中后面的示例之一给出了这方面的示例。
var ref = new Firebase("https://dinosaur-facts.firebaseio.com/dinosaurs");
ref.orderByChild("height").equalTo(25).on("child_added", function(snapshot) {
console.log(snapshot.key());
});
当应用于您的问题时,它转换为:
var ref = new Firebase('https://<your-app>.firebaseio.com/user');
var things = ref.child(yourUserId).child('things');
var query = things.orderByChild('feature').equalTo('something');
query.on('child_added', function(snapshot) {
console.log('The value of the thing is now: '+JSON.stringify(snapshot.val()));
// We can remove this node with
// snapshot.ref().remove
// This will fire a child_removed event
// We can update this node with
// snapshot.ref().update({ name: 'Linda H' });
// This in turn will fire a child_changed event
});
如评论中所述,child_added
事件将针对现有子节点和在您 运行 查询之后添加的节点触发。本质上,它会为查询中的任何新内容触发。