jonathanhuo11
jonathanhuo11

Reputation: 758

Firebase.update failed : first argument contains undefined in property

I have a simple Firebase function that updates some data. However, the interpreter says that the first argument contains "undefined" in property 'users.tester1'. Can somebody help me please?

var objify = function() {
  var rv = {};
    for (var i = 0; i < arguments.length; ++i)
      rv[arguments[i]] = rv[arguments[i+1]];
    return rv;
}

    addUser("tester1", []);

var addUser = function(name, edges){
  if(!checkIfUsernameExists(name) && !checkIfNodeNameExists(name) && !checkIfEdgeNameExists(name)){
    var time = Firebase.ServerValue.TIMESTAMP;

    //HERE: I think the error is on this line 
    refs.users.update(objify(name, "filler"));

    refs.users.child(name).set({
      "id" : time,
      "edges" : "filler"
    });
    refs.users.child(name).child("edges").update({
      "to" : "filler",
      "from" : "filler"
    });

    addNode(new Node(name, time, name));

    for(var e in edges){
      refs.users.child(name).child("edges/to").update(objify(edges[e].name, true));
      addEdge(new Edge(edges[e].name, time, edges[e].to, edges[e].arrows));
      //TODO add a "from" edge so that you know who wants to eat you
    }
    refs.users.child(name).child("edges/to").set({"filler" : null});
  } else {
    alert("user/node/edge name taken.");
  }
};

Upvotes: 56

Views: 69878

Answers (5)

Marian07
Marian07

Reputation: 2580

I used the Object.keys() method:

var key1 = Object.keys(firstPaymentsData)[0],
    key2 = Object.keys(firstPaymentsData[key1])[0],
    uid  = firstPaymentsData[key1][key2].uid;

Upvotes: 0

Aamir
Aamir

Reputation: 176

The underlying Firebase library throws an error when you try to update a property with the value of undefined. It will allow null (and will essentially ignore them).

You'll need your raw transformer to ensure that it ignores all prototype methods and properties and converts undefined to null or removes any properties with with undefined values altogether.

var addUser = function(name, edges){
  if(!checkIfUsernameExists(name) && !checkIfNodeNameExists(name) && !checkIfEdgeNameExists(name)){
    var time = Firebase.ServerValue.TIMESTAMP;

    //HERE: I think the error is on this line 
    refs.users.update(objify(name, "filler"));

    refs.users.child(name).set({
      "id" : time || null,
      "edges" : "filler" || null
    });
    refs.users.child(name).child("edges").update({
      "to" : "filler" || null,
      "from" : "filler" || null
    });

    addNode(new Node(name, time, name));

Upvotes: 2

Jeremy Souffir
Jeremy Souffir

Reputation: 355

to make sure your object does not contain any undefined props use this simple trick:

JSON.parse( JSON.stringify(YourJsonData ) )

For more info take a look at this codePen: http://codepen.io/ajmueller/pen/gLaBLX

Upvotes: 30

garrettmac
garrettmac

Reputation: 8585

Like said above, you need all undefined values to be null if you want them to save as empty values in firebase.

I take this approach to correct all nested values.

  //to search and replace    
    const replaceAll  =(s="",f="",r="")=>  s.replace(new RegExp(f.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'), 'g'), r)

    //to save 
   firebase.database().ref(`path`).update(JSON.parse(replaceAll(JSON.stringify(val),"undefined","null")))

Upvotes: 0

Frank van Puffelen
Frank van Puffelen

Reputation: 599641

When you pass an object to Firebase, the values of the properties can be a value or null (in which case the property will be removed). They can not be undefined, which is what you're passing in according to the error.

Simply running this snippet in isolation shows the problem:

var objify = function() {
  var rv = {};
    for (var i = 0; i < arguments.length; ++i)
      rv[arguments[i]] = rv[arguments[i+1]];
    return rv;
}
objify("name", "filler")

Results in:

{name: undefined, filler: undefined}

My best bet is that you want to pass key/value pairs into objify as even/odd parameters. In that case you want to change the function to:

var objify = function() {
  var rv = {};
    for (var i = 0; i < arguments.length; i+=2)
      rv[arguments[i]] = arguments[i+1];
    return rv;
}
objify("name", "filler")

Results in:

{name: "filler"}

Upvotes: 59

Related Questions