![](/img/trans.png)
[英]Expected first argument to collection() to be a CollectionReference, a DocumentReference or FirebaseFirestore
[英]Cloud Functions: Expected first argument to collection() to be a CollectionReference, a DocumentReference or FirebaseFirestore
嗨,我寫了這個 function
const functions = require("firebase-functions");
const admin = require("firebase-admin");
const { setDoc, doc } = require("firebase/firestore");
admin.initializeApp();
const db = admin.firestore();
// // Create and Deploy Your First Cloud Functi
exports.idiot = functions.pubsub.schedule('* * * * *').onRun(async (context) => {
// // Add a new document with a generated id
const moment = require('moment')
console.log("testtesttest 603")
console.log(db.collection)
const Questions =
{
Questions: {
QoD: "test",
Upvotes: 0,
}
}
const Comments =
{
Comments: {
Body: "hello",
Replies: [],
Upvotes: 0
}
}
await setDoc(doc(db, "Questions", moment().format('MMM Do YYYY')), Questions);
await setDoc(doc(db, "Comments", moment().format('MMM Do YYYY')), Comments);
});
突然我收到這個錯誤:
2022-09-28T00:59:00.846793Z ? idiot: FirebaseError: Expected first argument to collection() to be a CollectionReference, a DocumentReference or FirebaseFirestore
2022-09-28T00:59:00.848764071Z D idiot: Function execution took 9 ms. Finished with status: error
但是它之前是在寫文檔。
之前腳本運行時是否會彈出此錯誤?
您在此處導入兩個不同的 SDK:
const admin = require("firebase-admin");
const { setDoc, doc } = require("firebase/firestore");
第一行導入 Firebase Admin SDK,它設計用於 Cloud Functions 等可信環境。
第二行從客戶端導入兩個函數 JavaScript SDK ,用於常規應用。
雖然兩個 SDK 的 API 大體相同,但兩者並不二進制兼容。 因此, db
變量指向Firestore
object 和 Admin SDK,而您的doc
function 期望來自客戶端 JavaScript SDK 的Firestore
object。
僅使用管理員 SDK:
const questionRef = db.doc("Questions", moment().format('MMM Do YYYY'));
await questionRef.set(Questions);
然后相當於評論。
在文檔中,您需要查找此語法的v8
代碼示例,例如文檔中有關寫入文檔參考的此處。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.