Pushing Data To A Child Node with AngularJS and Firebase - angularjs

I'm getting this:
"users" : {
"simplelogin:129" : {
"-JbX-CJuMDf1H8-yCETV" : {
"gold" : {
"receive" : {
"amount" : 1,
"date" : 1416835946409,
"email" : "you#me.com"
}
}
},
But I need something like this:
"users" : {
"simplelogin:130" : {
"date" : 1416834728422,
"email" : "yam#dam.com",
"id" : "130",
"gold" : {
"receive" : {
"-JbWva7Gh9Y3RarlF77h" : {
"amount" : 1,
"date" : 1416834737201,
"email" : "you#do.com"
},
If I use .update instead of .push, I get the file structure I need, but then it overrides it for each transaction instead of adding a new one each time.
receive.orderByChild("email").equalTo( $scope.user.email ).once('child_added', function(snap) {
snap.ref().push({gold: {receive: myGold}}).then(function() {
});

Your question is not very clear, but I assume that you want to push the new node deeper in the tree.
simplelogin:130
date: ...
email: "yam#dam.com"
id: 130
gold
received
-JbX-CJuMDf1H8-yCETV
amount: 1
date: ...
email: "yam#dam.com"
To put the data into gold/received, you'd do:
snap.ref().child('gold/receive').push(myGold);
So you first find the node you want to add something to and only then call push to add a new child (with a new unique id) there.

Related

firebase add data to database with array

I am trying to add a set of data to a database by pushing the data to an array and adding them to the database. I have added my codes here
const com = {
company: "Com",
description: "Turn ...",
pointsNeeded: "500",
title: "Com",
PromoCode: Math.floor(Math.random()*1000000000),
imageLocation: "www...",
}
this.afDB.list('rewards/' + se.uid).push(com)
.then(next => {
console.log(next);
});
This is how my database look like
However, I would like the database to be like
"rewards" : {
"fH7GPHMFupW2JD8fxKnLOvHBZ0w1" : {
0: {
"PromoCode" :
"company" :
"description" :
"imageLocation" :
"pointsNeeded" :
"title" :
},
1" : {
"PromoCode" :
"company" :
"description" :
"imageLocation" :
"pointsNeeded" :
"title" :
}
}
}
Can anyone advice? I am not really sure what to do next.
Thanks
UPDATE:
const getUserReward = firebase.database().ref('rewards/').child(se.uid)
getUserReward.once('value', data => {
var rewardArray =[];
rewardArray.push(comchestReward)
getUserReward.update({rewards: rewardArray})
})
So this is the code I have so far but it is now

Rename a sub-document field within an Array of an Document

I am trying to rename the particular child field inside an array of an collection in MONGODB
{
"_id" : Id("248NSAJKH258"),
"isGoogled" : true,
"toCrawled" : true,
"result" : [
{
"resultsId" : 1,
"title" : "Text Data to be writen",
"googleRanking" : 1,
"isrelated" : false
},
{
"resultId" : 2,
"title" : "Text Data",
"googleRanking" : 2,
"isrelated" : true
}]
**I need to rename "isrelated" to "related" ** from the collection document
Using mongo Version 4.0
I Tried :
db.collection_name.update({}, { $rename: { 'result.isrelated': 'result.related'} } )
But it didn't worked in my case
As mentioned in official documentation $rename not working for arrays.
Please check link below:
https://docs.mongodb.com/manual/reference/operator/update/rename/
But you can do something like this
let newResult = [];
db.aa1.find({}).forEach(doc => {
for (let i in doc.result) {
newResult.push({
"resultsId" : doc.result[i]['resultsId'],
"title" : doc.result[i]['title'],
"googleRanking" : doc.result[i]['googleRanking'],
"related" : doc.result[i]['isrelated'],
})
}
db.aa1.updateOne({_id: doc._id}, {$set: {result: newResult}});
newResult = []
})

Find Firebase Child from Array Values - xcode 8 / swift 3

Id like to get a snapshot with snapshot.key as the UID if the snapshot.value is "pending". Store that into an array. Then i'd like to loop through the array of UID and pull of all the details from .child("users") i.e. "email", "name" & "profileURL". Not sure what the best route is here. Do I store the snapshot into a dictionary then filter dictionary to "pending" or do this within the snapshot call itself?
friends & users JSON structure in my Firebase Database (as below;)
{
"friends" : {
"YPQYLtXnMbbmFugrJJPYe6rOIJg2" : {
"Z6PnyFKSR9MBMd9dfCEs0VMIOog2" : "pending"
},
"Z6PnyFKSR9MBMd9dfCEs0VMIOog2" : {
"YPQYLtXnMbbmFugrJJPYe6rOIJg2" : "pending",
"ZyAV7PH4VHWnLyWrWaZty5C9RWT2" : "pending",
"lNI9FxCErqMUNiW43yiDpkNoljg1" : "pending"
},
"ZyAV7PH4VHWnLyWrWaZty5C9RWT2" : {
"Z6PnyFKSR9MBMd9dfCEs0VMIOog2" : "pending"
},
"lNI9FxCErqMUNiW43yiDpkNoljg1" : {
"Z6PnyFKSR9MBMd9dfCEs0VMIOog2" : "pending"
}
},
"users" : {
"YPQYLtXnMbbmFugrJJPYe6rOIJg2" : {
"email" : "orsrzlqwmt_1488724206#tfbnw.net",
"name" : "Karen Alaedidibgghi Liangescu",
"profileURL" : "someURL"
},
"Z6PnyFKSR9MBMd9dfCEs0VMIOog2" : {
"email" : "rbwimniovp_1488724216#tfbnw.net",
"name" : "Patricia Alaefhcbebjid Warmansen",
"profileURL" : “someURL”
},
"ZyAV7PH4VHWnLyWrWaZty5C9RWT2" : {
"email" : "bhulxppahm_1488724211#tfbnw.net",
"name" : "Harry Alaeejdjagjga Greenestein",
"profileURL" : "someURL"
},
"lNI9FxCErqMUNiW43yiDpkNoljg1" : {
"email" : "axtinlmwes_1488724221#tfbnw.net",
"name" : "Maria Alaefehgadbdg Valtchanovsky",
"profileURL" : "someURL"
}
}
}
Any help greatly appreciated. Cheers!
Here's how you'd get started, once you have your parsed data you can use different methods to filter it which you can look up through different stackoverflow questions answered in the past.
let dbRef = FIRDatabase.database().reference.child("friends")
dbRef.observeSingleEvent(of: .value, with: { snapshot in
if let data = snapshot.value as? [String: [String: String]] {
for user in data {
// You'll now have a dictionary with neatly formatted values you can filter
}
}
})

AngularFire, access deep child links?

Using latest Angular with Firebase and I'm trying to access somewhat deep nested items from logged in users.
The structure looks something like this:
firebase -> users -> user-uid -> email/name/pass/lists/ (where lists have) -> list-uid -> item-uid -> item-name.
I want to access that last name-child (preferably through an ng-repeat since they're list items and I want them displayed on each users dashboard).
I've been trying different methods but the closest one are this:
$scope.user = $firebaseObject(ref.child('users').child(authData.uid));
And then in my html (for now):
<span>{{user.lists}}</span>
Which prints out something like: {"-uid":{"age":"21", "name":"martin"},{"-uid":{"age":"25", "name":"john"}}} etc etc etc.
The ref goes to:
var ref = new Firebase('https://url.firebaseio.com');
Any ideas? Thanks!
Edit:
I tried to add a ng-repeat like this:
<li ng-repeat="list in user.lists">{{list.name}}</li>
Which somewhat works, it shows me the objects I've put in which are static but not the ones "one level deeper"...
Edit 2:
Updated with .json for structure.
I can get the "me" out of name but not "mjölk" which are a level deeper:
"users" : {
"8a6da12a-0992-43f5-bb33-95ddd49fed8c" : {
"email" : "torsten#gmail.com",
"lists" : {
"-KDjNYC8GS2QFHYyp74L" : {
"age" : "other",
"name" : "me"
},
"8a6da12a-0992-43f5-bb33-95ddd49fed8c" : {
"-KDjN_NkTQmj_1R00xZL" : {
"done" : false,
"id" : 1458936351305,
"name" : "mjölk"
},
"-KDjN_r0WmT6xiZwadO5" : {
"done" : false,
"id" : 1458936351305,
"name" : "ägg"
},
"-KDjNagkBWmJRj6oQqc2" : {
"done" : false,
"id" : 1458936351305,
"name" : "rejv"
},
"-KDjNb5Fpak_9A_rvn8W" : {
"done" : false,
"id" : 1458936351305,
"name" : "kossor"
},
"-KDjOVtAFJoYVNUkapNC" : {
"done" : false,
"id" : 1458936351305,
"name" : "piss"
},
"-KDjOlemXR2eOoqPBh0v" : {
"done" : false,
"id" : 1458936677873,
"name" : "mjölk igen"
}
}
},
As I said in my comment already: part of your problem is caused by the fact that you are nesting lists. There are many reasons why the Firebase documentation recommends to nest data sparingly and this is one of them.
To unnest the data, you store the user profiles separately from the lists for each user:
"users" : {
"8a6da12a-0992-43f5-bb33-95ddd49fed8c" : {
"email" : "torsten#gmail.com",
}
},
"user_lists" : {
"-KDjNYC8GS2QFHYyp74L" : {
"age" : "other",
"name" : "me"
},
"8a6da12a-0992-43f5-bb33-95ddd49fed8c" : {
"-KDjN_NkTQmj_1R00xZL" : {
"done" : false,
"id" : 1458936351305,
"name" : "mjölk"
},
"-KDjN_r0WmT6xiZwadO5" : {
"done" : false,
"id" : 1458936351305,
"name" : "ägg"
},
"-KDjNagkBWmJRj6oQqc2" : {
"done" : false,
"id" : 1458936351305,
"name" : "rejv"
},
"-KDjNb5Fpak_9A_rvn8W" : {
"done" : false,
"id" : 1458936351305,
"name" : "kossor"
}
}
}
Now if you want to show a list of profile and the lists for the current user, you:
var ref = new Firebase('https://yours.firebaseio.com');
$scope.users = $firebaseArray(ref.child('users'));
$scope.lists = $firebaseArray(ref.child('user_lists').child(auth.uid));
And then in your HTML:
<p>Users:</p>
<ul>
<li ng-repeat="user in users">{{user.email}}</li>
</ul>
<p>Lists for you:</p>
<ul>
<li ng-repeat="list in lists">{{list.name}}</li>
</ul>

Mongoose update array property

I am trying to update a document via Mongoose, but it does not update the array property.
Here's an example document:
{
"_id" : "55da477a9bfc910e38zzccf2",
"projectname" : "ASong",
"owner" : "adam",
"tracks" : [{
"name" : "Bass",
"file" : "upload/Bass.mp3",
"volume" : "0.75",
"pan" : "0.65"
}, {
"file" : "upload/Drums.mp3",
"volume" : "0.4",
"pan" : "-0.75",
"name" : "Drums"
}
],
"users" : ["adam", "eve"]
}
if I pass to Mongoose an object and try to use it to update like this:
var id = req.body._id;
var editedProject = req.body;
delete editedProject._id;
console.log("TRACKS: " +JSON.stringify(editedProject.tracks));
Project.update({"_id": id}, editedProject, {"upsert": true}, function(err, proj) {
if (err) {
res.send(err);
}
res.json(proj);
});
Only the 1st level properties are updated, but not the array.
I've found some solutions that involve looping through the array elements and calling an update for each element, but I would like to avoid that and keep the update as a single operation.
Or is it better to avoid using arrays?

Resources