1

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);
}); 

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! The structure is being correctly but how can I access this object in the html. It seem the object is still null by the time the html page loads. I am using ng-repeat in the html but it seems like the population of the object is happening too late. Do you have any suggestion on how to make sure the object is populated
@noobie212 I'm not really familiar with Angular but it seems this answer may have what you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.