我正在尝试从Google Apps脚本写入Firebase。
我试试这个:
var act="https://SampleChat.firebaseIO-demo.com/users/fred/name.json";
UrlFetchApp.fetch(act,{"method":"post","payload":"test"});
我收到此错误:
Request failed for https://SampleChat.firebaseIO-demo.com/users/fred/name.json
returned code 400. Truncated server response:
{ "error" : "Invalid data; couldn't parse JSON object, array, or value.
Perhaps you're using invalid characters in your key names." }...
如何从GAS写入Firebase?(我做错了什么?)
答案 0 :(得分:2)
您必须
stringify
payload
个对象。
Here is a question and answer example
我测试过它确实有用。
function putToFire() {
var payload = '{ "first": "Foo", "last": "Bar" }';
var options = {
"method": "put",
"payload": payload
};
UrlFetchApp.fetch(
"https://SampleChat.firebaseIO-demo.com/users/fred/name.json",
options
);
}
请注意
'
对象周围的单引号payload
?
在以下行中:
var payload = '{ "first": "Foo", "last": "Bar" }';
这种违反直觉的语法似乎是让这项工作成功的诀窍。 ; - )
如果您的payload
是变量,则可以使用JSON.stringify()
:
var payload=JSON.stringify({"first":"Foo","last":"Bar"});