Tho*_*Bui 2 javascript express firebase google-cloud-functions google-cloud-firestore
我有一个页面,用户可以在其中输入他们的信息。当用户单击“保存”时,我会将所有内容打包成一个对象并使用 AJAX 将其发送到我的云函数。在那里,我使用 Express 框架设置了一个服务器,并将数据保存到 Firestore。
当我点击“保存”时,一切正常。我的数据显示在 Firestore 中并且没有错误。但是,当我编辑数据并再次单击“保存”(仍在同一页面上)时,我在服务器端收到此错误:
Error: Element at index 0 should not be an empty string.
at new FieldPath (C:\Users\Me\Documents\myProject\functions\node_modules\@google-cloud\firestore\build\src\path.js:440:23)
at validateDocumentData (C:\Users\Me\Documents\myProject\functions\node_modules\@google-cloud\firestore\build\src\write-batch.js:625:16)
at WriteBatch.set (C:\Users\Me\Documents\myProject\functions\node_modules\@google-cloud\firestore\build\src\write-batch.js:242:9)
at DocumentReference.set (C:\Users\Me\Documents\myProject\functions\node_modules\@google-cloud\firestore\build\src\reference.js:337:27)
at saveToFireStore (C:\Users\Me\Documents\myProject\functions\index.js:101:47)
Run Code Online (Sandbox Code Playgroud)
这是我在客户端遇到的错误:
localhost:5001/myProject/us-central1/app:1 POST http://localhost:5001/myProject/us-central1/app
415 (Unsupported Media Type)
Run Code Online (Sandbox Code Playgroud)
请注意,这在我第一次单击“保存”按钮时没有发生。无论数据是否更改,都会发生此错误。只要多次单击“保存”,就会始终出现此错误。
额外说明:我没有在代码中使用批处理语句,但错误显示对 WriteBatch() 的调用
我尝试做的事情:
我在服务器端进行了检查,以确保 obj 符合条件(非空,包含有效字段...)。
我使用了 express.json() 中间件。
我使用了我的数据对象的简化版本进行测试,但它仍然无法正常工作。例子如下;实际对象相同但更长。
obj = {
id: "123",
name: "John",
age: "24"
}
Run Code Online (Sandbox Code Playgroud)
如果用户离开页面并返回,“保存”按钮会多次起作用。但是,我希望用户能够在同一页面上保存、编辑任何内容并再次保存。
单击另一个云功能任务(我有一个“下载”按钮)并重试“保存”按钮也不起作用。
- 我在客户端的代码:
// extract data from input fields
let dataObj = getDisplayedData();
// I also tested using this
// let dataObj = {
// id: "123",
// name: "John",
// age: "24"
//}
let xhttp = new XMLHttpRequest();
const url = "cloud/function/url";
xhttp.open("POST", url);
xhttp.setRequestHeader("Content-Type", "application/json");
xhttp.send(JSON.stringify(dataObj));
Run Code Online (Sandbox Code Playgroud)
- 我在服务器端的代码:
Error: Element at index 0 should not be an empty string.
at new FieldPath (C:\Users\Me\Documents\myProject\functions\node_modules\@google-cloud\firestore\build\src\path.js:440:23)
at validateDocumentData (C:\Users\Me\Documents\myProject\functions\node_modules\@google-cloud\firestore\build\src\write-batch.js:625:16)
at WriteBatch.set (C:\Users\Me\Documents\myProject\functions\node_modules\@google-cloud\firestore\build\src\write-batch.js:242:9)
at DocumentReference.set (C:\Users\Me\Documents\myProject\functions\node_modules\@google-cloud\firestore\build\src\reference.js:337:27)
at saveToFireStore (C:\Users\Me\Documents\myProject\functions\index.js:101:47)
Run Code Online (Sandbox Code Playgroud)
检查您如何包装对象。刚刚在我自己的项目中遇到了这个,发现我正在抓取的 API 数据确实有一个带有空键的对象:
{ items: { '': [Object], A: [Object], B: [Object], C: [Object] }, size: 4 }
Run Code Online (Sandbox Code Playgroud)
从 items 对象中删除第一个元素为我“解决”了创建错误。
| 归档时间: |
|
| 查看次数: |
620 次 |
| 最近记录: |