Reputation: 13
Current I have a array of urls hard coded to receive images in the html.
$scope.product = {
"images" : [
{"image": "https://s3-us-west-2.amazonaws.com/s.cdpn.io/194929/colorburst-700x700.jpg"},
{"image": "https://s3-us-west-2.amazonaws.com/s.cdpn.io/194929/colorburst-700x700.jpg"},
{"image": "https://s3-us-west-2.amazonaws.com/s.cdpn.io/194929/colorburst-700x700.jpg"}
] };
I just learned how to recieve data from the database but how do I store the results in an json array like what is shown in the "$scope.product"
I am currently receiving the data via
// Get a reference to our posts
var ref = new Firebase("https://<database>.firebaseio.com/profile");
// Retrieve new posts as they are added to our database
ref.on("child_added", function(snapshot, prevChildKey) {
var newPost = snapshot.val();
debugger;
console.log("The email is: " + newPost.email);
});
Upvotes: 1
Views: 2878
Reputation: 16865
Keep in mind that javascript arrays and objects are not JSON arrays and dictionaries despite how closely they match up. It appears that you want to create a javascript object from the data as you receive it.
Given your code, adjusting it to something like this should produce a structure similar to what you're asking about:
// Get a reference to our posts
var ref = new Firebase("https://<database>.firebaseio.com/profile");
// some place to store stuff
var results = {
'emails':[]
};
// Retrieve new posts as they are added to our database
ref.on("child_added", function(snapshot, prevChildKey) {
var newPost = snapshot.val();
results.emails.push({
'email':newPost.email
});
});
Converting the javascript results
object to a JSON string is as simple as:
var json_string = JSON.stringify(results);
There are several ways to arrange the data. I'm only able to provide code for the known elements.
Upvotes: 1