【发布时间】:2018-12-23 02:58:19
【问题描述】:
创建新用户后,我想将数据添加到 Firestore 中的新集合中。
我的功能是这样设置的;
exports.createUser = functions.firestore.document('Users/{userId}')
.onCreate((snap, context) => {
const newValue = snap.data();
if (snap.data() === null) return null;
const userRef = context.params.userId
console.log("create user found " + (userRef))
let notificationCollectionRef = firestoreDB.collection('Users').document(userRef).collection('Notifications')
let notificationDocumentRef = notificationCollectionRef.document('first notification')
return notificationDocumentRef.set({
notifications: "here is the notificiation"
}, {merge: true});
});
运行该函数时,我得到控制台日志正确打印出用户 ID,但出现以下错误;
TypeError: firestoreDB.collection(...).document 不是函数 在exports.createUser.functions.firestore.document.onCreate (/user_code/index.js:23:73) 在对象。 (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:112:27) 在下一个(本机) 在 /user_code/node_modules/firebase-functions/lib/cloud-functions.js:28:71 在 __awaiter (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:24:12) 在 cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:82:36) 在 /var/tmp/worker/worker.js:728:24 在 process._tickDomainCallback (internal/process/next_tick.js:135:7)
我是 JS 和函数的新手。一如既往,非常感谢任何帮助。
【问题讨论】:
-
let notificationCollectionRef = snap.ref.collection('Notifications') -
或者...
let firestoreDB = snap.ref.firestore -
@JamesPoag 得到同样的错误。
标签: node.js firebase google-cloud-firestore google-cloud-functions