![](/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.