【发布时间】:2020-01-16 12:53:56
【问题描述】:
我正在做一个 react 项目,这是我的第一个 react 项目。此代码已成功部署。但是在使用邮递员进行测试时会出现一些错误。我“发布”“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)
【问题讨论】:
-
错误提示您正在发送和
undefined值。您可以在 POST 中粘贴您发送的正文吗? -
只要确保在通过邮递员发送请求时添加 Content-Type:application/json 标头
-
如果您使用 Postman 发出请求,则不清楚这与 React 有何关系。
-
@ErandiDilshani 它与 firebase.json 无关 :) 。我说的是邮递员。您需要指定 Content-Type:application/json 标头 learning.getpostman.com/docs/postman/sending_api_requests/…
标签: javascript firebase google-cloud-functions postman