我有一个问题。 Firebase 似乎为使用 push() 方法创建的对象生成唯一 ID。虽然这很神奇,但官方教程似乎忽略了这样一个简单的问题,即如何根据对象的唯一 ID检索这样的对象。
{
"contacts" : {
"-KD3p6kdEUbg38X7Osfm" : {
"address" : "Whitley rd 22, Northampton",
"email" : "somethingsatsomething@soso",
"name" : "George Booney",
"notes" : "Sounds like a serious businessman",
"phone" : "01239485764",
"website" : "http://www.workingatthepumps.com"
},
"-KD3pzA_P3pK0TCzg4zw" : {
"address" : "Common Avenue 22, Birmingham",
"email" : "johnyoeias@nowhereas.com",
"name" : "John Doe",
"notes" : "Sounds like a serious businessman",
"phone" : "44582314864",
"website" : "http://www.wearelost.com"
}
}
}
调用它的代码如下所示:
var ref = new Firebase("https://contactsmgr.firebaseIO.com/contacts");
return {
get: function() {
return ref;
},
find: function() {
// the required code to refer to the unique id
}
};
是否有一种简单的方法可以做到这一点,或者我创建的 JSON 结构本身就是错误的?
最佳答案
如果你有 key ,你可以像这样访问ref.child(id)
。
正如@FrankvanPuffelen 所阐明的那样,您然后将其绑定(bind)到您的 AngularJS 范围:
$scope.contact = $firebaseObject(ref.child('-KD3pzA_P3pK0TCzg4zw'));
如果您还没有 key ,并且需要通过姓名或电子邮件查找,那么您可以使用 orderBy
和 startAt
和 endAt
:
ref.orderByChild('phone').startAt('01239485764').endAt('01239485764');
关于javascript - 通过它在 Firebase 中的唯一 ID 查找对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36067964/