使用firebase admin sdk时,firebase云函数中未定义引用错误firestore

使用firebase admin sdk时,firebase云函数中未定义引用错误firestore,firebase,google-cloud-firestore,google-cloud-functions,Firebase,Google Cloud Firestore,Google Cloud Functions,我有一个云函数,其中我正在侦听firestore集合的onCreate事件。当云函数被触发时,我得到一个参考错误。下面是云功能代码和错误 const functions=require('firebase-functions')) const servicecomport=require('./servicecomportkey.json') const admin=require('firebase-admin') const{firestore}=require('firebase-admi

我有一个云函数,其中我正在侦听firestore集合的onCreate事件。当云函数被触发时,我得到一个参考错误。下面是云功能代码和错误

const functions=require('firebase-functions'))
const servicecomport=require('./servicecomportkey.json')
const admin=require('firebase-admin')
const{firestore}=require('firebase-admin')
admin.initializeApp({
凭证:管理员凭证证书(serviceAccount),
数据库URL:'https://my-app-id.firebaseio.com“,//为了隐私,我在这里替换了我的真实id
})
const db=admin.firestore()
exports.handleDiscussionReplyAdded=functions.firestore
.document('/discussionId/{discussionId}/reples/{replyId}'))
.onCreate(异步(快照、上下文)=>{
试一试{
//1.创建对父聊天文档的引用
const discussionRef=db
.collection(“讨论”)
.doc(context.params.discussionId)
//2.更新数据
等待讨论参考更新({
totalReplies:firestore.FieldValue.increment(1),
})
//如果一切顺利,则返回true
返回真值
}捕获(错误){
//如果出现错误,则返回错误
functions.logger.error(`error:${JSON.stringify(error)}`)
返回错误
}
})
{
“严重性”:“错误”,
“消息”:“ReferenceError:firestore未在/Users/syedalirazabokhari/Desktop/Development/React/omanshing/functions/index.js:23:23\n在cloudFunction(/Users/syedalirazabokhari/Desktop/Development/React/omanshing/functions/node_modules/firebase functions/lib/cloud functions.js:134:23)定义”\n在/usr/local/lib/node_modules/firebase tools/lib/emulator/functionsemulatoruntime.js:590:16\n在runFunction(/usr/local/lib/node_modules/firebase tools/lib/emulator/functionsemulatoruntime.js:577:15)\n在runBackground(/usr/local/lib/node_modules/firebase tools/lib/emulator/functionsemulatoruntime.js:589:11)\n位于processBackground(/usr/local/lib/node_modules/firebase tools/lib/emulator/functionsEmulatorRuntime.js:572:11)\n位于invokeTrigger(/usr/local/lib/node_modules/firebase tools/lib/node_modules/firebase-emulator/functionsEmulatorRuntime.js:647:19)\n位于handleMessage(/usr/local/lib/lib/lib/lib/emulator/functionsEmulatorRuntime/functionerruntime.js:\n在处理和拒绝时(内部/process/task_queues.js:93:5)”
}

我试图调试该问题,但无法解决。我还将firebase admin更新到目前为止的最新版本9.5.0。

删除不必要的导入
const{firestore}=require('firebase-admin')
,然后将
firestore.FieldValue.increment(1)
更改为
admin.firestore.FieldValue.increment(1)
修复了错误。

我不认为这是导致问题的原因,但多次使用同一模块似乎不寻常。你有没有考虑过<代码> const FixSturt= admin?FixSturt?我还有其他的云函数,它们工作得很好,但是当这个特定函数被触发时,我得到了这个引用错误。是的,你是对的,我没有注意到多个进口。我将尝试删除一个,可能这就是原因。我删除了导入,但问题仍然存在。我认为(firestore.FieldValue.increment(1))这一行有问题,但我不确定。知道吗?我甚至把它改成了admin.firestore.FieldValue.increment(1)。但是没有luckI从另一个项目更新index.js。多亏了vscode。顺便说一下,这个问题现在已经解决了。谢谢@FrankvanPuffelen