云功能-根据警告更新时间戳

时间:2018-11-08 02:44:32

标签: node.js firebase google-cloud-firestore google-cloud-functions

我为Firebase Cloud Functions进行了以下设置:

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

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

我收到以下警告,并对在云功能端和客户端的代码中需要进行哪些更改感到好奇:

TypeError: db.settings is not a function
at Object.<anonymous> (/Users/troychuinard/Code/FanPolls/functions/index.js:13:4)
at Module._compile (module.js:643:30)
at Object.Module._extensions..js (module.js:654:10)
at Module.load (module.js:556:32)
at tryModuleLoad (module.js:499:12)
at Function.Module._load (module.js:491:3)
at Module.require (module.js:587:17)
at require (internal/module.js:11:18)
at /usr/local/lib/node_modules/firebase-tools/lib/triggerParser.js:21:11
at Object.<anonymous> (/usr/local/lib/node_modules/firebase-tools/lib/triggerParser.js:61:3)

注意:我可能需要更新我的Cloud Functions,但是我很好奇这将如何影响index.js和trackVote.js中的代码

1 个答案:

答案 0 :(得分:2)

const admin = require('firebase-admin');
const functions = require('firebase-functions');
let firebaseApp;
if (admin.apps.length > 0) {
  firebaseApp = admin.app()
} else {
  firebaseApp = admin.initializeApp(functions.config().firebase)
}

let db = firebaseApp.firestore()
db.settings({ timestampsInSnapshots: true })