0

I am wondering whether its possible to add data to firebase using a for loop because that data I am working with is more or less the same but the number increments by 1. Currently what I am doing is;

var usersRef = ref.child("users");
usersRef.set({
  Person1: {
    name: "person1",
    surname: "person1"
  },
  Person2: {
    name: "person2",
    surname: "person2"
  },
  Person3: {
    name: "person3",
    surname: "person3"
  },
  .........
  .........
  Person28: {
    name: "person28",
    surname: "person28"
  }
});

What I would instead want to do is:

    var ref = new Firebase("https://docs-examples.firebaseio.com/web/saving-data/fireblog");
    var usersRef = ref.child("users");
 for (var i = 0; i <= 28; ++i) {
    usersRef.set({
    Person+i: {
    name: "person" + i,
    surname: "person" + i
    }
   });
}

Is it possible to something like this? any help is welcomed.

3 Answers 3

2

Sure:

var person = {};

for (var i = 1; i <= 28; ++i) {
    person[ "Person" + i ] = { "name": "person" + i, surname: "person" + i };
}

var ref = new Firebase("https://docs-examples.firebaseio.com/web/saving-data/fireblog");
var usersRef = ref.child("users");
usersRef.set( person );

NB: index should start at 1

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

6 Comments

Thanks @llya I will try this now and let you know how I get along, quick question does 'var usersRef = ref.child("users");' have to be inside or outisde for loop?
@Henry sorry, I didn't pay attention to the way the object was constructed (only to the prop. set the syntax), I've fixed the code.
Hi, this only seem to take the last number of the loop (28) and therefore overwriting previous data.
It should work, but there's a typo ("name": "person" + 1 instead of "name": "person" + i)
@Henry I've fixed the typo and tested, it's working for me.
|
0

I don't see why not but I don't recommend newing up Firebase on each iteration unless the docs explicitly specify to do so. And the same goes for the child getter. I would move them to outside the loop like so:

var ref = new Firebase("https://docs-examples.firebaseio.com/web/saving-data/fireblog");
var usersRef = ref.child("users");
for (var i = 0; i <= 28; ++i) {
    var persons = {};
    persons['Person'+i] = {
        name: "person" + i,
        surname: "person" + i
    }
}
usersRef.set(persons);

The bracket notation can be used for getting/setting properties programmatically as well as using keys that are invalid JavaScript.

4 Comments

but how can I increment each record by 1 using the loop, when I use 'Person+i' it does not work.
Sorry, missed the point of the question entirely. See the update.
Hi, this only seem to take the last number of the loop (28) and therefore overwriting previous data.
@user2687646 you should declare var persons = {} outside of the for loop.
0

In your current approach (even if you fix the syntax errors), each new user that you add replaces the previous user(s), since you're calling usersRef.set(). If you use update() instead of set, it will not overwrite the existing data.

But this seems simpler to me:

var ref = new Firebase("https://yours.firebaseio.com/");
var usersRef = ref.child("users");
for (var i = 0; i <= 28; ++i) {
    usersRef.child("Person"+i).set({
        name: "person" + i,
        surname: "person" + i
    });
}

Comments

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.