2

I am using firebase(angularfire) in my angularjs app to store and process my message system but can't seem to figure out how to replicate the example data from the firebase docs

 // room members are easily accessible (or restricted)
      // we also store these by room ID
      "members": {
        // we'll talk about indices like this below
        "one": {
          "mchen": true,
          "hmadi": true
        }
      }

Here the members.one contains the user name as a key and I am trying to do this for my data as well but can't seem to figure out a solution.

The members portion of my firebase data is like so:

members { one: { } }

I have two variables set in the $scope.

user_name = kep; //person chatting with name
sender_name = pek; //current user name

So I want to use the set function to insert data into members.one or in this case members.user_name + ':' + sender_name but where I am having trouble is how to actually insert the data without creating a parent object.

ref.child('members').child(user_name + ':' + sender_name).set({
    user_name: true, sender_name: true
 });

The problem arises when I try to pass user_name and sender_name into the set() function below is the result it gets.

members { "kep:pek": { user_name: true, sender_name: true }}

where as I want it to be:

members { "kep:pek": { kep: true, pek: true }}

If I put user_name and sender_name into an object and then run the set() function with the object passed it will create the following structure which is not what I am looking for:

members { "kep:pek": { newObject: { kep: true, pek: true }}}

1 Answer 1

2

Firebase team member here.

The Firebase Database is a just a JSON document.

So let's say you want to structure your data this way:

{
  "members" : {
    "kep:pek" : {
      "kep" : true,
      "pek" : true
    }
  }
}

A custom key is created by using the .child() method, or by creating a key in the JavaScript Object.

JSBin Demo

var rootRef = new Firebase('<my-firebase-app>');
var membersRef = rootRef.child('members');
var user_name = 'kep';
var sender_name = 'pek';
// child object to store custom keys
var objectToSave = {};
// set keys in [] syntax
objectToSave[user_name] = true;
objectToSave[sender_name] = true;
// use .child() with a formatted string to save the object
membersRef.child(user_name + ':' + sender_name).set(objectToSave);
Sign up to request clarification or add additional context in comments.

8 Comments

Hi David, thanks for taking the time to help. I was trying to do the above but it is inserting as an object and not just key value pairs. It insert like so members.one.obj { user_name: true } and not members.one { user_name: true } The users are being generated on click (chat rooms) and thats why the user_name and sender_name need to be passed
Anywhere where you need a key you can use .child(), and then set the dat a needed at that path. At the end of the day everything is just one JSON object. I updated my answer to represent that.
When I insert using the above it creates the following structure: members: { one: { objectToSave: { "mchen": true } } } where as I want members: { one: { "mchen": true } } the only problem I keep running into is that when inserting like my second example how do I pass user_name to show up as mchen? - @David East
I'm confused now. In your first comment you said didn't want members.one { user_name: true } and now you're saying that's what you want. Regardless of what you want though, it's all just a key/value store. Calling .child(keyName) with a key name will get you what you want.
Sorry for the confusion, I re-worded my question maybe it makes more sense now ?
|

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.