Introduction:
Firebase is a powerful real-time database solution offered by Google as part of the Google Cloud Collective. However, developers often encounter challenges while working with Firebase, such as the “Firebase.update failed: first argument contains undefined in property” error. In this blog post, we will explore the reasons behind this error and discuss how to troubleshoot and resolve it effectively. By understanding the root cause of this issue, developers can enhance their Firebase development experience and build robust applications. So let’s dive in!
Understanding the Error:
The error message “Firebase.update failed: first argument contains undefined in property” indicates that the first argument passed to the update method in Firebase contains undefined values. Firebase does not accept undefined values and expects either a valid value or null for properties. Let’s take a closer look at the code snippet that triggers this error:
codevar 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.");
}
};
Analyzing the Code:
Upon analyzing the code, we can identify that the objify function is responsible for constructing an object with arguments passed to it. However, due to a logical error, undefined values are being assigned to object properties. To resolve this issue, we need to correct the objify function. Additionally, the addUser function further triggers the error by passing the erroneous object to the refs.users.update method.
Fixing the Error:
To fix the error, we can modify the objify function as follows:
codevar objify = function() {
var rv = {};
for (var i = 0; i < arguments.length; i += 2)
rv[arguments[i]] = arguments[i+1];
return rv;
}
In this updated version of the objify function, we iterate over the arguments with a step of 2, ensuring that each even-indexed argument represents the property name, and the subsequent odd-indexed argument represents its corresponding value. This modification ensures that undefined values are not assigned to properties.
Next, we need to fix the addUser function by ensuring it calls the corrected objify function and passes a valid object to the refs.users.update method. Here’s the updated code snippet:
codevar addUser = function(name, edges){
if(!checkIfUsernameExists(name) && !checkIfNodeNameExists(name) && !checkIfEdgeNameExists(name)){
var time = Firebase.ServerValue.TIMESTAMP;
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.");
}
};
Conclusion:
In this blog post, we explored the “Firebase.update failed: first argument contains undefined in property” error and learned how to troubleshoot and resolve it. By understanding the root cause and implementing the necessary code modifications, developers can successfully update data in Firebase without encountering this error. Remember to ensure that object properties do not contain undefined values and pass a valid object to Firebase update methods. With these insights, you can enhance your Firebase development workflow and build reliable applications.

