问题描述
当Firebase宣布。我有一个相当复杂的数据结构,需要写入几个位置来管理实体之间的关系,所以多路径更新在完美的时间来临 - 我不必担心做几个背靠背写/更新(和有Firebase的风险)。所以,我把所有东西都编码好了,并且执行了一个多路径更新(使用Rest API),只是被下面的响应放下: / p> error:数据无效;无法解析JSON对象,数组或值。在你的关键名字。
我看了,查看我是否使用了无效字符(。
, $
,#
, [
,]
, /
)。我没有(除了正斜杠之外,这是一个超级新的和真棒多路径更新,允许在这个关键的字符)。
我在Chrome中快速打开Javascript控制台,在我的字符串上运行 JSON.parse()
,并将其解析为一个有效的表,那么什么是问题?
我的更新包含以下内容:
{
foo / bar:{
data:{
baz:1
}
},
foo / bar / data:{
quu:2
}
}
在尝试使用官方的[email protected]频道之后,我采取了蛮力的调试方式。
我将每个键/值对分别解析为自己的表,并试图进行多路径更新,并在每次工作时尝试。当我知道我是奇怪的东西。然后我慢慢建立了整个表键,直到多路径更新失败,我看到了问题。
我的更新包含以下内容:
{
foo / bar:{
data:{
baz:1
$ b $ f $ / b
$ qu $ 2
$ b
我希望在Firebase中产生的数据:
foo {
bar:{
data:{
baz:1,$ b $ quu:2
}
}
}
}
答案是,多路径更新不能包含写入同一位置的两个关键字名称(或位于同一路径中更深处的位置)。
所以,答案是结合两个多路径更新密钥写入Firebase中的相同位置如下所示:
{
foo / bar / data:{
baz:1,
quu:2
}
}
I was excited when Firebase announced multi-path updates a while back. I have a fairly complex data structure that requires writes to several locations for managing relationships between entities, so multi-path updates came at the perfect time - I didn't have to worry about making several back-to-back writes/updates (and risk having Firebase rate-limit me).
So, I coded everything up, buckled in, and performed a multi-path update (using the Rest API), only to be let down by the following response:
"error" : "Invalid data; couldn't parse JSON object, array, or value. Perhaps you're using invalid characters in your key names."
I looked at the docs to see if I was using invalid characters (.
, $
, #
, [
, ]
, /
) in any key names. I wasn't (other than the forward-slash becuase this was the super new and awesome multi-path update that allowed for such a character in the key).
I quickly opened up the Javascript console in Chrome, ran JSON.parse()
on my string, and it parsed into a valid table, so what's the problem?
My update contained the following:
{
"foo/bar": {
"data": {
"baz": 1
}
},
"foo/bar/data": {
"quu": 2
}
}
After an unsuccessful attempt at using the official [email protected] channel, I took to brute force debugging.
I parsed out each key/value pair separately as its own table and attempted a multi-path update, and each time it worked. This is when I knew I was onto something odd. Then I slowly built up the entire table key by key until the multi-path update failed and I saw the issue.
My update contained the following:
{
"foo/bar": {
"data": {
"baz": 1
}
},
"foo/bar/data": {
"quu": 2
}
}
And I was hoping for the resulting data in Firebase:
{
foo: {
bar: {
data: {
baz: 1,
quu: 2
}
}
}
}
So, the simple answer is, a multi-path update cannot contain two key names that write to the same location (or a location deeper in the same path).
Now, my multi-path update contained upwards of 20 key/value pairs, so it wasn't quite as easy to spot as the example I've laid out here, so cut me a little slack. I can understand for a number of reasons why this may not be allowed (the atomicity of the request, which update gets applied first, etc), but my issue is that the error returned from Firebase was not only not helpful, it flat out pointed me in the wrong direction, making debugging even harder.
So, the answer is to combine the two multi-path update keys that write to the same location in Firebase to look like the following:
{
"foo/bar/data" : {
"baz": 1,
"quu": 2
}
}
这篇关于Firebase多路径更新“无效的数据;无法解析JSON对象,数组或值“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!