云函数日志错误:admin.firestore.doc不是函数

时间:2018-11-10 03:09:07

标签: node.js firebase

尝试运行云功能时收到以下错误:

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});
                                                  }
                                                });
                                        });
                 });
           });

我愿意更新到最新版本的云功能,但是我担心,根据我读过的其他文章,它将使我当前的所有代码无效。

1 个答案:

答案 0 :(得分:1)

这是您正确执行此操作的一行:

const voteCountRef = admin.firestore().doc(`Polls/${context.params.pollId}`);

这不是您所在的行:

const trendScoreRef = admin.firestore.doc(`Polls/${context.params.pollId}/trend_score`);

您只需要在()之后添加admin.firestore