首页 > 解决方案 > 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});
                                                  }
                                                });
                                        });
                 });
           });

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

标签: node.jsfirebase

解决方案


这是您正确执行的一行:

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

这是你不在的那一行:

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

您只需要()admin.firestore.


推荐阅读