node.js - Cloud Functions 日志错误:admin.firestore.doc 不是函数
问题描述
尝试运行我的云功能时收到以下错误:
TypeError: admin.firestore.doc is not a function
at exports.handler.functions.firestore.document.onCreate (/user_code/trackVote.js:10:57)
at Object.<anonymous> (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:112:27)
at next (native)
at /user_code/node_modules/firebase-functions/lib/cloud-functions.js:28:71
at __awaiter (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:24:12)
at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:82:36)
at /var/tmp/worker/worker.js:758:24
at process._tickDomainCallback (internal/process/next_tick.js:135:7)
我不确定为什么会收到错误消息,我的功能正在正确部署:
index.js
const functions = require('firebase-functions');
const trackVote = require('./trackVote')
const admin = require('firebase-admin');
admin.initializeApp();
exports.trackVote = trackVote.handler;
trackVote.js
const functions = require('firebase-functions');
const admin = require('firebase-admin');
exports.handler = functions.firestore.document('/Polls/{pollId}/responses/{userId}').onCreate((data, context) => {
const answerSelected = data.data().answer;
const answerRef = admin.firestore().doc(`Polls/${context.params.pollId}/answers/${answerSelected}`);
const voteCountRef = admin.firestore().doc(`Polls/${context.params.pollId}`);
const trendScoreRef = admin.firestore.doc(`Polls/${context.params.pollId}/trend_score`);
return admin.firestore().runTransaction(t => {
return t.get(answerRef)
.then(doc => {
if (doc.data()) {
t.update(answerRef, { vote_count: doc.data().vote_count + 1 });
}
})
}).then(result => {
return admin.firestore().runTransaction(t => {
return t.get(voteCountRef)
.then(doc => {
if (doc.data()) {
t.update(voteCountRef, {vote_count:doc.data().vote_count+1});
}
});
});
//starting with this set, I believe this code has caused the issue
}).then(result => {
return admin.firestore().runTransaction(t => {
return t.get(trendScoreRef)
.then(doc => {
if (doc.data()) {
t.update(trendScoreRef, {trend_score:doc.data().trend_score+1});
}
});
});
});
});
我愿意更新到最新版本的云功能,但我担心,根据我读过的其他帖子,它会使我当前的所有代码无效。
解决方案
这是您正确执行的一行:
const voteCountRef = admin.firestore().doc(`Polls/${context.params.pollId}`);
这是你不在的那一行:
const trendScoreRef = admin.firestore.doc(`Polls/${context.params.pollId}/trend_score`);
您只需要()
在admin.firestore
.
推荐阅读
- bash - 在 shell 或 Makefile (AWS) 中使用 JSON 字符串中的 HTML 文件内容
- spring - 弹簧配置条件测试
- r - 如何从 ggplotly 图表中获取选定的事件数据?
- elasticsearch - 无法在 Elasticsearch 中向 master 发送加入请求,未知 NamedWriteable [org.elasticsearch.cluster.metadata.MetaData$Custom][licenses]]
- r - 为什么 tidyr `complete()` 没有在 R 中完成?
- python - 如何为墙壁添加边界?
- python - 为什么这段代码没有完全运行?它不运行第 53 行
- python - 使用 WebDav API 和 Python 请求下载 NextCloud 文件
- react-i18next - react-i18next 中“cimode”的含义是什么,为什么没有正确记录?
- javascript - How to add a conditional scroll based on window size?