我无法弄清楚如何定位 firebase 数据库中的单个对象。例如,我希望在单击索引卡时显示特定的单词/定义。我用它来存储数据:
wordVal = $("#word").val();
defVal = $("#def").val();
data = firebase.database().ref("indexCards");
data.push( { 'word': wordVal, 'definition': defVal } );
这是检索它:
data.on("child_added", function(snapshot){
console.log(snapshot.val().word);
console.log(snapshot.val().definition);
});
这给了我完整的单词和定义列表。我想分别提到特定的词和特定的定义。文档说我可以通过这样做来引用特定值 - firebase.database().ref("child/path")
。
但我的问题是......当 parent 是那些随机数字和字母(见下文)时,我如何引用路径?我知道这些是由 firebase 生成的唯一 ID,但我不知道如何像访问普通对象一样访问它们。
{
"-KQIOHLNsruyrrnAhqis" : {
"definition" : "person, place, thing",
"word" : "noun"
},
"-KQIOO7Wtp2d5v2VorqL" : {
"definition" : "device for photos",
"word" : "camera"
},
"-KQISp4WMnjABxQayToD" : {
"definition" : "circus act",
"word" : "clown"
},
"-KQITC9W1lapBkMyiL7n" : {
"definition" : "device used for cutting",
"word" : "scissors"
}
}
最佳答案
您可以使用这样的查询:
data = firebase.database().ref('indexCards').orderByChild('word').equalTo('noun')
这是不言自明的,我们得到对 indexCards 的引用,其中键 word 的值等于 noun,因此它将返回键为 -KQHZ3xCHTQjuTvonSwj< 的对象/em>
链接到 Firebase docs: Retrieve data - Sorting and Filtering data
关于javascript - 如何定位 firebase 数据库中的单个值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39195774/