尝试将 google 云函数与 node.js 一起使用时,admin.firestore 不是函数
admin.firestore is not a function when trying to use google cloud functions with node.js
这是 node.js index.js 文件的 header:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
var db = admin.firestore();
这是监听 firestore 变化的 node.js 函数:
exports.myFoo = functions.firestore
.document('foo/{bar}')
.onWrite(event => {
// do stuff
}
这在 package.json 文件中:
"dependencies": {
"firebase-admin": "^5-.4.2",
"firebase-functions": "^0.7.1",
"firestore": "^1.1.6"
},
当我尝试执行“firebase 部署”命令时,这是我收到的错误:
Error: Error occurred while parsing your function triggers.
TypeError: admin.firestore is not a function
问Firebase
我能够重现错误并暴力破解解决方案。我对 npm
了解不多,无法完整解释此解决方案为何有效。
我的原文package.json
包含:
"dependencies": {
...
"firebase-admin": "^4.2.1",
"firebase-functions": "^0.7.1",
...
},
作为recommended in the documentation,我运行函数文件夹中的这两个命令:
npm install -g firebase-tools
npm install firebase-functions@latest --save
我也试过:
npm install --save firebase-admin
npm upgrade
我反复收到这些错误消息:
+-- UNMET PEER DEPENDENCY firebase-admin@4.2.1
npm WARN firebase-functions@0.7.1 requires a peer of firebase-admin@~5.4.2 but none was installed.
我认为 firebase-admin
需要更新,但无法实现。所以我编辑了依赖文件以删除这一行:
"firebase-admin": "^4.2.1"
然后 运行 再 npm install --save firebase-admin
。这样,package.json
包含版本 "firebase-admin": "^5.4.2"
和 var db = admin.firestore();
编译没有错误。
在你的file.js(异步方法)中解决:
const getFirestore = () => admin.firestore()
例如:
getFirestore()
.collection(`mailchimp-users`)
.doc(uid)
.set(profile)
.then(() => res.redirect(MAILCHIMP_AUTH_SUCCESS_URL))
.catch((error) => res.json({error}))
functions.firestore
应该是 functions.firestore()
试试这个
const getReceiverDataPromise = admin.firestore().doc('users/' + receiverUID).get();
const getSenderDataPromise = admin.firestore().doc('users/' + senderUID).get();
return Promise.all([getReceiverDataPromise, getSenderDataPromise]).then(results => {
const receiver = results[0].data();
console.log("receiver: ", receiver);
const sender = results[1].data();
console.log("sender: ", sender);
});
这是 node.js index.js 文件的 header:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
var db = admin.firestore();
这是监听 firestore 变化的 node.js 函数:
exports.myFoo = functions.firestore
.document('foo/{bar}')
.onWrite(event => {
// do stuff
}
这在 package.json 文件中:
"dependencies": {
"firebase-admin": "^5-.4.2",
"firebase-functions": "^0.7.1",
"firestore": "^1.1.6"
},
当我尝试执行“firebase 部署”命令时,这是我收到的错误:
Error: Error occurred while parsing your function triggers.
TypeError: admin.firestore is not a function
问Firebase
我能够重现错误并暴力破解解决方案。我对 npm
了解不多,无法完整解释此解决方案为何有效。
我的原文package.json
包含:
"dependencies": {
...
"firebase-admin": "^4.2.1",
"firebase-functions": "^0.7.1",
...
},
作为recommended in the documentation,我运行函数文件夹中的这两个命令:
npm install -g firebase-tools
npm install firebase-functions@latest --save
我也试过:
npm install --save firebase-admin
npm upgrade
我反复收到这些错误消息:
+-- UNMET PEER DEPENDENCY firebase-admin@4.2.1
npm WARN firebase-functions@0.7.1 requires a peer of firebase-admin@~5.4.2 but none was installed.
我认为 firebase-admin
需要更新,但无法实现。所以我编辑了依赖文件以删除这一行:
"firebase-admin": "^4.2.1"
然后 运行 再 npm install --save firebase-admin
。这样,package.json
包含版本 "firebase-admin": "^5.4.2"
和 var db = admin.firestore();
编译没有错误。
在你的file.js(异步方法)中解决:
const getFirestore = () => admin.firestore()
例如:
getFirestore()
.collection(`mailchimp-users`)
.doc(uid)
.set(profile)
.then(() => res.redirect(MAILCHIMP_AUTH_SUCCESS_URL))
.catch((error) => res.json({error}))
functions.firestore
应该是 functions.firestore()
试试这个
const getReceiverDataPromise = admin.firestore().doc('users/' + receiverUID).get();
const getSenderDataPromise = admin.firestore().doc('users/' + senderUID).get();
return Promise.all([getReceiverDataPromise, getSenderDataPromise]).then(results => {
const receiver = results[0].data();
console.log("receiver: ", receiver);
const sender = results[1].data();
console.log("sender: ", sender);
});