我正在嘗試使用 Node.js Firebase Admin SDK 將新的 javascript 對象添加到 Firestore 集合中的數組字段。我確定我要定位的文檔有問題的字段,但我不斷收到錯誤:TypeError:Cannot read property 'arrayUnion' of undefined我知道這個問題,但修復沒有幫助。非常感激任何的幫助。有問題的路線:router.post("/create-new-user-group", async (req, res) => { try { const { userId, usersName, groupName } = req.body; if (!userId || !usersName || !groupName) return res.status(422).send(); const groupRef = await db.collection("groups").doc(); const group = await groupRef.set({ id: groupRef.id, name: groupName, userId, members: [{ id: userId, name: usersName, isAdmin: true }], }); const response = await db .collection("users") .doc(userId) .update({ groups: fbApp.firestore.FieldValue.arrayUnion({ id: userId, name: userName, }), }); res.send(group); } catch (error) { console.log("error creating new group:", error); res.status(400).send(error); }});firebaseInit.js:const admin = require("firebase-admin");const serviceAccount = require("../serviceAccount.json");const firebaseApp = admin.initializeApp({ credential: admin.credential.cert(serviceAccount), databaseURL: "https://xxx-xxxxx.firebaseio.com",});exports.fbApp = firebaseApp;
如何在Firestore中正確使用arrayUnion?
阿波羅的戰車
2022-12-22 10:01:02