火力地堡通过JavaScript API第键检索对象 [英] Firebase retrieve Object by Key in JavaScript Api
问题描述
帮助:
我火力地堡的目的和因为关键是一个的ObjectID和该文档的api JS火力地堡不能从列表恢复只有一个项目,它不必为我恢复的方法。
I'm an object of Firebase and can not recover only one item from the list because the key is a objectId and the doc api JS Firebase, it does not have a method to recover for me.
你能帮助我吗?
控制器
var rooms = Rooms.all();
console.log('All Rooms:', rooms);
console.log('Rooms length:', rooms.length);
// New test room
if (!rooms) {
var objRooms = [
{
cc: 'Business 1',
name: 'Chat 1',
city: 'Curitiba',
state: 'PR'
},
{
cc: 'Business 2',
name: 'Chat 2',
city: 'Floripa',
state: 'SC'
}
]
var objRoom = Rooms.save(objRooms);
console.log('ROOMID: ', objRoom.key());
}
服务
.factory('Rooms', function ($firebaseObject) {
// Might use a resource here that returns a JSON array
var ref = new Firebase(firebaseUrl);
var rooms = $firebaseObject(ref.child('rooms'));
return {
all: function () {
return rooms;
},
get: function (roomId) {
// Simple index lookup
console.log('ROOMD:', rooms);
return rooms[ ref.child(roomId) ];
},
save: function (roomData) {
var obj = ref.child('rooms');
var onComplete = function (error) {
if (error) {
console.log('Data could not be saved: ', error);
}
else {
console.log('Data saved successfully!');
}
};
return obj.push(roomData, onComplete);
}
}
})
输出:
Chat Controller initialized!
controllers.js:117 Object {params: Object, current: Object, $current: extend, transition: null}$current: extendcurrent: Objectget: (stateOrName, context)go: go(to, params, options)href: href(stateOrName, params, options)includes: includes(stateOrName, params, options)is: is(stateOrName, params, options)params: ObjectroomId: "-JxlCvzgbdkQfoA1Of78"__proto__: Objectreload: reload()transition: nulltransitionTo: transitionTo(to, toParams, options)__proto__: Object
services.js:78 Selecting the room with id: -JxlCvzgbdkQfoA1Of78
services.js:20 ROOMD: d {$$conf: Object, $id: "rooms", $priority: null}
app.js:43 Logged in as : simplelogin:2
工厂间是有问题的:
The factory Rooms is a problem:
get: function (roomId) {
// Simple index lookup
console.log('ROOMS:', rooms);
console.log('ROOM specified:', ref.child(roomId).key());
return rooms[ ref.child(roomId) ];
},
[更新]
我创建过滤器对象数据工厂对象:
I created the factory Objects for filter Object data:
.factory('Objects', function () {
return {
filter: function (objectValues, objectKey) {
// to take an action after the data loads, use the $loaded() promise
objectValues.$loaded().then(function () {
// To iterate the key/value pairs of the object, use angular.forEach()
angular.forEach(objectValues, function (value, key) {
if (key === objectKey) {
console.log(objectValues[objectKey]);
return objectValues[objectKey];
}
});
});
}
}
})
和客房
工厂,我编辑的方法 GET
,并设置:
and Rooms
factory, I edited method get
and set:
get: function (roomId) {
return Objects.filter(rooms, roomId);
},
[更新]
我有一个基于数据库的图像中的:
[Update] I have a database based in the image:
我需要列表对象数据。
JavaScript API第
https://www.firebase.com/docs/web/api/
推荐答案
你应该使用Angularfire它提供使用火力地堡与AngularJS一些有用的方法首先。你可以在这里找到的文档:
First of all you should use Angularfire which provides some useful methods for using Firebase with AngularJS. You can find the documentation here:
https://www.firebase.com/docs/web/库/角/ api.html
现在,你的问题是如何使用它的密钥来访问的单个项目。您可以通过检索时,只是提供链接到该对象做,只是无论是 $ firebaseArray
或 $ firebaseObject
(当使用Angularfire API):
Now, your question is how to access an single item using it's key. You can do that simply by just providing the url to that object when retrieving either a $firebaseArray
or $firebaseObject
(when using Angularfire API):
http://myfirebase.firebase.io/blazing-heat-9118/rooms/ + roomId
然后通过这个url到新火力地堡()
电话:
var ref = new Firebase('http://myfirebase.firebase.io/blazing-heat-9118/rooms/' + roomId);
var room = $firebaseObject(ref);
// To take an action after the data has finished loading, use the $loaded() promise
obj.$loaded().then(function(res) {
console.log(res); // res will be the room object
// To iterate the key/value pairs of the object, use angular.forEach()
angular.forEach(obj, function(value, key) {
console.log(key, value);
});
});
现在你将获取从数据库中单间。
Now you will have fetched the single room from the database.
我建议通过Angularfire文档阅读彻底,因为它包含了用于处理您的数据一些伟大的方法。
I'd suggest reading through the Angularfire documentation thoroughly as it contains some great methods for handling your data.
这篇关于火力地堡通过JavaScript API第键检索对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!