尝试在node.js中使用google cloud函数时,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:
错误:解析函数触发器时发生错误.
TypeError: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中使用google cloud函数时,admin.firestore不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!