我对Firebase Cloud函数有以下设置:
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)
注意:我可能需要更新我的云函数,我可以这样做,但我很好奇这会对我的index.js和trackVote.js 中的代码产生什么影响
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 })