Javascript Firebase.update 失败:第一个参数在属性中包含未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34708566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Firebase.update failed : first argument contains undefined in property
提问by jonathanhuo11
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?
我有一个简单的 Firebase 函数来更新一些数据。但是,解释器说第一个参数在属性“users.tester1”中包含“undefined”。有人可以帮我吗?
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.");
}
};
回答by Frank van Puffelen
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.
当您将对象传递给 Firebase 时,属性的值可以是一个值或 null(在这种情况下,该属性将被删除)。它们不能是undefined
,这是您根据错误传递的内容。
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:
我最好的选择是您希望将键/值对objify
作为偶数/奇数参数传递。在这种情况下,您要将函数更改为:
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"}
{名称:“填充物”}
回答by Jeremy Souffir
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
有关更多信息,请查看此 codePen:http://codepen.io/ajmueller/pen/gLaBLX
回答by garrettmac
Like said above, you need all undefined
values to be null
if you want them to save as empty values in firebase.
如上所述,如果您希望它们在 firebase 中保存为空值,则需要所有undefined
值null
。
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")))