![](/img/trans.png)
[英]Flutter Firestore : Add a List of Data to Firestore Document
[英]Cloud Firestore document add gives error "Value for argument "data" is not a valid Firestore document. Cannot use "undefined" as a Firestore value"
我正在做一个反应项目,这是我的第一个反应项目。 此代码已成功部署。 但是在使用 postman 进行测试时会出现一些错误。 我“发布”“createScream”函数的 URL 并发送。 然后我得到了这个错误。
任何对解决问题的帮助都非常感谢。 我是反应世界的新手。 这是我的 index.json 文件
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
//
exports.helloWorld = functions.https.onRequest((request, response) => {
response.send("Hello world");
});
exports.getScreams = functions.https.onRequest((req, res)=> {
admin
.firestore()
.collection('screams')
.get()
.then(data=>{
let screams =[];
data.forEach(doc =>{
screams.push(doc.data());
});
return res.json(screams);
})
.catch((err)=> console.error(err));
});
exports.createScream = functions.https.onRequest((req, res)=> {
const newScream = {
body:req.body.body,
userHandle: req.body.userHandle,
createdAt: admin.firestore.Timestamp.fromDate(new Date())
};
admin
.firestore()
.collection('screams')
.add(newScream)
.then((doc)=>{
res.json({message:'document ${doc.id} created successfully'});
})
.catch((err)=>{
res.status(500).json({ error:'something went wrong'});
console.error(err);
});
我收到此错误消息
Error: Value for argument "data" is not a valid Firestore document. Cannot use "undefined" as a Firestore value (found in field body).
at Object.validateUserInput (E:\survival\TF2\functions\node_modules\@google-cloud\firestore\build\src\serializer.js: 273: 15)
at Object.validateDocumentData (E:\survival\TF2\functions\node_modules\@google-cloud\firestore\build\src\write-batch.js: 611: 22)
at CollectionReference.add (E:\survival\TF2\functions\node_modules\@google-cloud\firestore\build\src\reference.js: 1765: 23)
at exports.createScream.functions.https.onRequest (E:\survival\TF2\functions\index.js: 38: 6)
at Run (C:\Users\User\AppData\Roaming\npm\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js: 608: 20)
at C:\Users\User\AppData\Roaming\npm\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js: 582: 19
at Generator.next (<anonymous>)
at C:\Users\User\AppData\Roaming\npm\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js: 7: 71
at new Promise (<anonymous>)
at __awaiter (C:\Users\User\AppData\Roaming\npm\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js: 3: 12)
问题在于这段代码:
const newScream = {
body:req.body.body,
userHandle: req.body.userHandle,
createdAt: admin.firestore.Timestamp.fromDate(new Date())
};
admin
.firestore()
.collection('screams')
.add(newScream)
当您尝试向 Cloud Firestore 添加属性值之一为undefined
的文档时,您将收到此错误。 这意味着newScream
的三个属性中至少有一个是未定义的。 错误消息告诉您是哪一个:
不能使用“未定义”作为 Firestore 值(在字段正文中找到)
因此,body 属性是未定义的。 您需要调试您的函数并找出req.body.body
未定义的原因。 也许客户端没有传递该值。
我的情况的问题是使用userHandle ,而在用户中我使用了handle 。 我刚刚将尖叫帖子中的值从 userHandle 重命名为 just handle,见下文:
前
const newScream = {
body: req.body.body,
userHandle: req.user.userHandle,
createdAt: new Date().toISOString(),
};
修正后
const newScream = {
body: req.body.body,
userHandle: req.user.handle,
createdAt: new Date().toISOString(),
};
我也尝试相同的编码,我解决了这个问题。
在邮递员中,您将看到错误Cannot use "undefined" as a Firestore value (found in field body).
但没关系。 因为它在您的帖子 URL 中没有任何数据。
你可能会误以为是屏幕看到的。 单击 URL 输入位置下的body tag
。 然后选择raw
。 最后切换到JSON,您的问题将得到解决。 将JSON数据写入下图红线。
然后,在写入 JSON 数据并按[send]
,您将能够在屏幕下方message :~~ successfully
看到message :~~ successfully
。
尝试一下!
要解决此问题,请在 Firestore 实例上使用ignoreUndefinedProperties
设置。
这会在序列化期间跳过未定义的属性,以便它们不会首先写入 Firestore 文档:
import * as admin from 'firebase-admin';
admin.initializeApp();
const firestore = admin.firestore();
firestore.settings({ ignoreUndefinedProperties: true });
const doc = firestore.doc(`demo`);
doc.create({ id: 1, description: undefined }); // works without exception
就我而言,我将来自postman的请求作为原始数据发送,将数据类型更改为json使其工作!
我正在做你工作的同一个项目。 我遇到了同样的错误,只是重写了 createdScream 代码,重新启动邮递员并创建新的 POST 请求。 然后发送新的请求。 那个有效。
我也遇到过这个问题。
原来我犯的错误是在
const newScream = {
body:req.body.body,
userHandle: req.body.userHandle,
createdAt: admin.firestore.Timestamp.fromDate(new Date())
};
userHandle
中的userHandle
在req.body.userHandle
json 中有小写user
{
'body':'new',
'UserHandle': 'new'
}
发出的请求有UserHandle
大写。
我通过改变固定的问题userHandle: req.body.userHandle
在JS为大写UserHandle: req.body.UserHandle
。
确保const newScream
中的任何const newScream
与您尝试在const newScream
json 中发送的内容完全匹配。
我有同样的问题,我通过在 Postman 的方法“POST”的正文中选择“x-www-form-urlencoded”找到了另一个替代解决方案。 修改键和值然后发送。
所以,我只是做了firebase deploy ,而不是firebase serve ,一切正常,我能够添加 newScream 。
确保您在邮递员中使用了正确的端点
对于每个 firebase 函数都应该有一个唯一的端点
例如:对于getScreams
函数,您有一个链接
https://us-central1-chat-app-xmx.cloudfunctions.net/getScreams
对于createScreams
函数,您将有另一个链接,例如:
https://us-central1-chat-app-xmx.cloudfunctions.net/createScreams
现在,您应该在邮递员中用于post request
的链接是createScreams
端点,对于get request
您需要使用getScreams
端点
您得到的错误不是什么大问题,因为该过程需要客户端输入一些数据,但是您需要使用正确的端点才能将您的文档成功发送到 firestore
检查以确保在 Postman 或您正在使用的任何其他 API 中选择了 JSON(application/json)。
对我来说,解决问题的方法是修复 post 请求中给createdAt
属性的值。 通过给_seconds
和_nanoseconds
:
"createdAt":{"_seconds":1623472200,"_nanoseconds":0},
而不是仅仅给予时间价值
"createdAt": "10:00pm",
Undefined 在 Firestore 中不是可接受的值。 您设置的数据需要是 object {}
而不是数组[]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.