尝试在 node.js 中使用谷歌云函数时,admin.firestore 不是一个函数 [英] admin.firestore is not a function when trying to use google cloud functions with node.js
问题描述
这是 node.js index.js 文件的标题:
This is the header of the node.js index.js file:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
var db = admin.firestore();
这是用于监听 firestore 变化的 node.js 函数:
This is the node.js function to listen to firestore changes:
exports.myFoo = functions.firestore
.document('foo/{bar}')
.onWrite(event => {
// do stuff
}
这是在 package.json 文件中:
This is in the package.json file:
"dependencies": {
"firebase-admin": "^5-.4.2",
"firebase-functions": "^0.7.1",
"firestore": "^1.1.6"
},
当我尝试进行firebase 部署"时命令这是我得到的错误:
When I try to do a "firebase deploy" command this is the error I am getting:
错误:解析函数触发器时出错.
类型错误:admin.firestore 不是函数
Error: Error occurred while parsing your function triggers.
TypeError: admin.firestore is not a function
askFirebase
推荐答案
我能够重现错误并强力解决方案.我对 npm
知之甚少,无法完整解释此解决方案为何有效.
I was able to reproduce the error and brute-force a solution. I don't know much about npm
and can't offer a complete explanation of why this solution worked.
我原来的 package.json
包含:
"dependencies": {
...
"firebase-admin": "^4.2.1",
"firebase-functions": "^0.7.1",
...
},
作为在文档中推荐,我在功能文件夹:
As recommended in the documentation, I ran these two commands in the functions folder:
npm install -g firebase-tools
npm install firebase-functions@latest --save
我也试过:
npm install --save firebase-admin
npm upgrade
我反复收到这些错误消息:
I repeatedly received these error messages:
+-- 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
需要更新,但无法实现.所以我编辑了依赖文件以删除这一行:
I figured firebase-admin
needed to be updated but couldn't make it happen. So I edited the dependencies file to delete this line:
"firebase-admin": "^4.2.1"
然后再次运行 npm install --save firebase-admin
.这样,package.json
包含版本 "firebase-admin": "^5.4.2"
和 var db = admin.firestore();代码> 编译没有错误.
then ran npm install --save firebase-admin
again. With that, the package.json
contained version "firebase-admin": "^5.4.2"
and var db = admin.firestore();
compiled without error.
这篇关于尝试在 node.js 中使用谷歌云函数时,admin.firestore 不是一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!