尝试在node.js中使用google cloud函数时,admin.firestore不是函数 [英] admin.firestore is not a function when trying to use google cloud functions with node.js

查看:112
本文介绍了尝试在node.js中使用google cloud函数时,admin.firestore不是函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆