我有一个以Cloud Firestore为数据库的项目。现在我想使用fetch方法更新一个文档中的数据。我的Cloud Firestore结构如下:
Logging (Collection)
[userID] (Document)
Notifications (Collection)
[notificationID] (Document)
active: "true"
type: "T1"
我使用下面的提取调用:
fetch("https://firestore.googleapis.com/v1/projects/[ID]/databases/(default)/documents/Logging/[userID]
+"/Notifications/[notificationID]?updateMask.fieldPaths=active", {
method: 'PATCH',
body: JSON.stringify({
"active": "false"
}),
headers: {
Authorization: 'Bearer ' + idToken,
'Content-Type': 'application/json'
}
}).then( function(response){
console.log(response);
response.json().then(function(data){
console.log(data);
});
}).catch(error => {
console.log(error);
});
执行我正在运行的获取方法时出现错误,消息为
"收到无效的JSON负载。未知名称"活动的";在"document"处:找不到字段">
如何使用REST API更新Firestore文档的现有字段?有人能帮我吗?我尝试了很多不同的";url";和方法,但没有什么对我有效。
如Firestore REST API文档中所述,您需要在主体中传递Document
类型的对象,如下所示:
{
method: 'PATCH',
body: JSON.stringify({
fields: {
active: {
stringValue: 'false',
},
},
}),
}
我假设您的active
字段的类型为String(因为您使用"active": "false"
(。如果它是Boolean类型,则需要使用booleanValue
属性,如下所示。有关更多详细信息,请参阅此文档。
{
method: 'PATCH',
body: JSON.stringify({
fields: {
active: {
booleanValue: false,
},
},
}),
}