Reputation: 21
I have a list / an array of objects organized numerically which i consider the object name as an 'id'. I am using firebase and I can't figure out how to grab the id number itself. Any help would be great
//js
myFirebaseRef.once("value", function(snapshot) {
var list = snapshot.val();
$scope.items = list
console.log($scope.items)
})
//log $scope.items
[1: Object, 2: Object, 3: Object, 4: Object, 5: Object, 6: Object, 7: Object //...
Upvotes: 1
Views: 2109
Reputation: 65
This is a real live example, using react and axios to retrieving the name of the object in firebase :
componentDidMount() {
axios.get('/uri.json')
.then(res => {
let fetchedData = [];
for (let key in res.data) {
fetchedData.push({
...res.data[key],
id: key //The key here holds the object's name !
});
}
});
}
Upvotes: 0
Reputation: 18595
Like this. You use obj[key] = value;
to set the id with a variable.
function update(node,key,value){
var ref = firebase.database().ref('/');
var obj = {};
obj[key] = value;
ref.child(node).update(obj)
.then(function() {
console.log('Update Ran Successfully');
});
}
Upvotes: 0
Reputation: 599766
Is this what you're looking for?
myFirebaseRef.once("value", function(snapshot) {
//var list = snapshot.val();
snapshot.forEach(function(itemSnapshot) {
console.log(itemSnapshot.key); // if you're using 2.x that is key()
});
})
Upvotes: 1
Reputation: 6878
with angular.forEach
like that you can do something near :
angular.forEach($scope.items,function(key,value){
//the key will contain your id
console.log(key);
})
Upvotes: 1