Cloud Functions - Updating Timestamps Based on Warning











up vote
0
down vote

favorite












I have the following setup for 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});
}
});
});
});
});


I received the below warning and was curious what changes I need to make within my code on both the cloud functions side and the client side:



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)


Note: I may need to update my Cloud Functions, which I can do, however I am curious how that would impact the code in both my index.js and trackVote.js










share|improve this question




















  • 1




    Please don't show pictures of error messages or code. It's better to copy an paste them into the question so they're easier to read and search.
    – Doug Stevenson
    Nov 8 at 5:45















up vote
0
down vote

favorite












I have the following setup for 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});
}
});
});
});
});


I received the below warning and was curious what changes I need to make within my code on both the cloud functions side and the client side:



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)


Note: I may need to update my Cloud Functions, which I can do, however I am curious how that would impact the code in both my index.js and trackVote.js










share|improve this question




















  • 1




    Please don't show pictures of error messages or code. It's better to copy an paste them into the question so they're easier to read and search.
    – Doug Stevenson
    Nov 8 at 5:45













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have the following setup for 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});
}
});
});
});
});


I received the below warning and was curious what changes I need to make within my code on both the cloud functions side and the client side:



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)


Note: I may need to update my Cloud Functions, which I can do, however I am curious how that would impact the code in both my index.js and trackVote.js










share|improve this question















I have the following setup for 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});
}
});
});
});
});


I received the below warning and was curious what changes I need to make within my code on both the cloud functions side and the client side:



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)


Note: I may need to update my Cloud Functions, which I can do, however I am curious how that would impact the code in both my index.js and trackVote.js







node.js firebase google-cloud-firestore google-cloud-functions






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 0:14

























asked Nov 8 at 2:44









tccpg288

1,14511533




1,14511533








  • 1




    Please don't show pictures of error messages or code. It's better to copy an paste them into the question so they're easier to read and search.
    – Doug Stevenson
    Nov 8 at 5:45














  • 1




    Please don't show pictures of error messages or code. It's better to copy an paste them into the question so they're easier to read and search.
    – Doug Stevenson
    Nov 8 at 5:45








1




1




Please don't show pictures of error messages or code. It's better to copy an paste them into the question so they're easier to read and search.
– Doug Stevenson
Nov 8 at 5:45




Please don't show pictures of error messages or code. It's better to copy an paste them into the question so they're easier to read and search.
– Doug Stevenson
Nov 8 at 5:45












1 Answer
1






active

oldest

votes

















up vote
2
down vote













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





share|improve this answer























  • Thanks, so this code would go in the index.js, below admin.initializeApp() but above exports.trackVote = trackVote.handler? Also, would I have to change all of the dates in my client code?
    – tccpg288
    Nov 8 at 3:12










  • Yes. You don't need to change dates in your client code, as long as it fits your needs.
    – Socrates Tuas
    Nov 8 at 3:18










  • Much appreciated
    – tccpg288
    Nov 8 at 3:20










  • Actually I encountered the error above, not sure if I should delete the first admin.initializeApp()
    – tccpg288
    Nov 8 at 3:40










  • Thanks, I tried your code and am still getting the above error. I realize I may need to update my cloud functions, just unsure how it will impact trackVote.js, if that will change at all.
    – tccpg288
    Nov 10 at 0:14











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53200823%2fcloud-functions-updating-timestamps-based-on-warning%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote













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





share|improve this answer























  • Thanks, so this code would go in the index.js, below admin.initializeApp() but above exports.trackVote = trackVote.handler? Also, would I have to change all of the dates in my client code?
    – tccpg288
    Nov 8 at 3:12










  • Yes. You don't need to change dates in your client code, as long as it fits your needs.
    – Socrates Tuas
    Nov 8 at 3:18










  • Much appreciated
    – tccpg288
    Nov 8 at 3:20










  • Actually I encountered the error above, not sure if I should delete the first admin.initializeApp()
    – tccpg288
    Nov 8 at 3:40










  • Thanks, I tried your code and am still getting the above error. I realize I may need to update my cloud functions, just unsure how it will impact trackVote.js, if that will change at all.
    – tccpg288
    Nov 10 at 0:14















up vote
2
down vote













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





share|improve this answer























  • Thanks, so this code would go in the index.js, below admin.initializeApp() but above exports.trackVote = trackVote.handler? Also, would I have to change all of the dates in my client code?
    – tccpg288
    Nov 8 at 3:12










  • Yes. You don't need to change dates in your client code, as long as it fits your needs.
    – Socrates Tuas
    Nov 8 at 3:18










  • Much appreciated
    – tccpg288
    Nov 8 at 3:20










  • Actually I encountered the error above, not sure if I should delete the first admin.initializeApp()
    – tccpg288
    Nov 8 at 3:40










  • Thanks, I tried your code and am still getting the above error. I realize I may need to update my cloud functions, just unsure how it will impact trackVote.js, if that will change at all.
    – tccpg288
    Nov 10 at 0:14













up vote
2
down vote










up vote
2
down vote









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





share|improve this answer














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






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 8 at 3:58

























answered Nov 8 at 3:07









Socrates Tuas

1146




1146












  • Thanks, so this code would go in the index.js, below admin.initializeApp() but above exports.trackVote = trackVote.handler? Also, would I have to change all of the dates in my client code?
    – tccpg288
    Nov 8 at 3:12










  • Yes. You don't need to change dates in your client code, as long as it fits your needs.
    – Socrates Tuas
    Nov 8 at 3:18










  • Much appreciated
    – tccpg288
    Nov 8 at 3:20










  • Actually I encountered the error above, not sure if I should delete the first admin.initializeApp()
    – tccpg288
    Nov 8 at 3:40










  • Thanks, I tried your code and am still getting the above error. I realize I may need to update my cloud functions, just unsure how it will impact trackVote.js, if that will change at all.
    – tccpg288
    Nov 10 at 0:14


















  • Thanks, so this code would go in the index.js, below admin.initializeApp() but above exports.trackVote = trackVote.handler? Also, would I have to change all of the dates in my client code?
    – tccpg288
    Nov 8 at 3:12










  • Yes. You don't need to change dates in your client code, as long as it fits your needs.
    – Socrates Tuas
    Nov 8 at 3:18










  • Much appreciated
    – tccpg288
    Nov 8 at 3:20










  • Actually I encountered the error above, not sure if I should delete the first admin.initializeApp()
    – tccpg288
    Nov 8 at 3:40










  • Thanks, I tried your code and am still getting the above error. I realize I may need to update my cloud functions, just unsure how it will impact trackVote.js, if that will change at all.
    – tccpg288
    Nov 10 at 0:14
















Thanks, so this code would go in the index.js, below admin.initializeApp() but above exports.trackVote = trackVote.handler? Also, would I have to change all of the dates in my client code?
– tccpg288
Nov 8 at 3:12




Thanks, so this code would go in the index.js, below admin.initializeApp() but above exports.trackVote = trackVote.handler? Also, would I have to change all of the dates in my client code?
– tccpg288
Nov 8 at 3:12












Yes. You don't need to change dates in your client code, as long as it fits your needs.
– Socrates Tuas
Nov 8 at 3:18




Yes. You don't need to change dates in your client code, as long as it fits your needs.
– Socrates Tuas
Nov 8 at 3:18












Much appreciated
– tccpg288
Nov 8 at 3:20




Much appreciated
– tccpg288
Nov 8 at 3:20












Actually I encountered the error above, not sure if I should delete the first admin.initializeApp()
– tccpg288
Nov 8 at 3:40




Actually I encountered the error above, not sure if I should delete the first admin.initializeApp()
– tccpg288
Nov 8 at 3:40












Thanks, I tried your code and am still getting the above error. I realize I may need to update my cloud functions, just unsure how it will impact trackVote.js, if that will change at all.
– tccpg288
Nov 10 at 0:14




Thanks, I tried your code and am still getting the above error. I realize I may need to update my cloud functions, just unsure how it will impact trackVote.js, if that will change at all.
– tccpg288
Nov 10 at 0:14


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53200823%2fcloud-functions-updating-timestamps-based-on-warning%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()