How to get data from Firestore in Cloud Function?
I need to know what I'm doing wrong here?
I'm calling this function from Flutter. The call back is getting done correctly and the first & second prints are coming in the "log" on Firbase. But getting undefined from the "Firestore"!!
This is the code in the Cloud Function:
var functions = require("firebase-functions");
let admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
admin.firestore().settings({ timestampsInSnapshots: true });
exports.storeContact5 = functions.https.onCall((data, context) => {
// First print is working fine
console.log('test');
var recieverId = 'WqHxLoYvRxR9UK8sFJZ9WxTOIE32';
const check = admin.firestore().collection('users').doc(recieverId).get();
check.then(testValue => {
console.log(testValue.data.nickname);
return true;
}).catch(err => {
console.log('Error getting document', err);
});
console.log('test2');
// Return to flutter App (Working fine)
return {
repeat_message: 'ok!'
}
});
Screenshot for Firebase Log
firebase google-cloud-firestore google-cloud-functions
add a comment |
I need to know what I'm doing wrong here?
I'm calling this function from Flutter. The call back is getting done correctly and the first & second prints are coming in the "log" on Firbase. But getting undefined from the "Firestore"!!
This is the code in the Cloud Function:
var functions = require("firebase-functions");
let admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
admin.firestore().settings({ timestampsInSnapshots: true });
exports.storeContact5 = functions.https.onCall((data, context) => {
// First print is working fine
console.log('test');
var recieverId = 'WqHxLoYvRxR9UK8sFJZ9WxTOIE32';
const check = admin.firestore().collection('users').doc(recieverId).get();
check.then(testValue => {
console.log(testValue.data.nickname);
return true;
}).catch(err => {
console.log('Error getting document', err);
});
console.log('test2');
// Return to flutter App (Working fine)
return {
repeat_message: 'ok!'
}
});
Screenshot for Firebase Log
firebase google-cloud-firestore google-cloud-functions
@RahulMahadik - This is Cloud Function code!
– Ahmed Abosrie
Nov 20 '18 at 19:31
add a comment |
I need to know what I'm doing wrong here?
I'm calling this function from Flutter. The call back is getting done correctly and the first & second prints are coming in the "log" on Firbase. But getting undefined from the "Firestore"!!
This is the code in the Cloud Function:
var functions = require("firebase-functions");
let admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
admin.firestore().settings({ timestampsInSnapshots: true });
exports.storeContact5 = functions.https.onCall((data, context) => {
// First print is working fine
console.log('test');
var recieverId = 'WqHxLoYvRxR9UK8sFJZ9WxTOIE32';
const check = admin.firestore().collection('users').doc(recieverId).get();
check.then(testValue => {
console.log(testValue.data.nickname);
return true;
}).catch(err => {
console.log('Error getting document', err);
});
console.log('test2');
// Return to flutter App (Working fine)
return {
repeat_message: 'ok!'
}
});
Screenshot for Firebase Log
firebase google-cloud-firestore google-cloud-functions
I need to know what I'm doing wrong here?
I'm calling this function from Flutter. The call back is getting done correctly and the first & second prints are coming in the "log" on Firbase. But getting undefined from the "Firestore"!!
This is the code in the Cloud Function:
var functions = require("firebase-functions");
let admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
admin.firestore().settings({ timestampsInSnapshots: true });
exports.storeContact5 = functions.https.onCall((data, context) => {
// First print is working fine
console.log('test');
var recieverId = 'WqHxLoYvRxR9UK8sFJZ9WxTOIE32';
const check = admin.firestore().collection('users').doc(recieverId).get();
check.then(testValue => {
console.log(testValue.data.nickname);
return true;
}).catch(err => {
console.log('Error getting document', err);
});
console.log('test2');
// Return to flutter App (Working fine)
return {
repeat_message: 'ok!'
}
});
Screenshot for Firebase Log
firebase google-cloud-firestore google-cloud-functions
firebase google-cloud-firestore google-cloud-functions
edited Nov 21 '18 at 8:52


KENdi
5,7892821
5,7892821
asked Nov 20 '18 at 19:05


Ahmed AbosrieAhmed Abosrie
12
12
@RahulMahadik - This is Cloud Function code!
– Ahmed Abosrie
Nov 20 '18 at 19:31
add a comment |
@RahulMahadik - This is Cloud Function code!
– Ahmed Abosrie
Nov 20 '18 at 19:31
@RahulMahadik - This is Cloud Function code!
– Ahmed Abosrie
Nov 20 '18 at 19:31
@RahulMahadik - This is Cloud Function code!
– Ahmed Abosrie
Nov 20 '18 at 19:31
add a comment |
1 Answer
1
active
oldest
votes
You should do testValue.data().nickname
and not testValue.data.nickname
, see https://firebase.google.com/docs/firestore/query-data/get-data#get_a_document and https://firebase.google.com/docs/reference/js/firebase.firestore.DocumentSnapshot#data.
Also, you should return a result only once and you should not return outside of the .then()
if you want to return the result of the asynchronous operation.
In addition, see here how to handle errors: https://firebase.google.com/docs/functions/callable#handle_errors
So you may do as follows:
exports.storeContact5 = functions.https.onCall((data, context) => {
// First print is working fine
console.log('test');
var recieverId = 'WqHxLoYvRxR9UK8sFJZ9WxTOIE32';
const check = admin.firestore().collection('users').doc(recieverId).get();
return check.then(testValue => {
console.log(testValue.data().nickname);
return {repeat_message: 'ok!'};
}).catch(err => {
console.log('Error getting document', err);
throw new functions.https.HttpsError('Error getting document', err);
});
});
I would suggest you watch the videos from the official series: https://firebase.google.com/docs/functions/video-series/, in particular the ones titled "Learn JavaScript Promises"
Thanks a lot :)) All working now
– Ahmed Abosrie
Nov 20 '18 at 19:55
@AhmedAbosrie Welcome to StackOverflow. Glad that I could help! You may accept my answer by clicking on the grey checkmark next to the answer and turn it to green, see stackoverflow.com/help/someone-answers.
– Renaud Tarnec
Nov 20 '18 at 20:06
add a comment |
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',
autoActivateHeartbeat: false,
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53399870%2fhow-to-get-data-from-firestore-in-cloud-function%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
You should do testValue.data().nickname
and not testValue.data.nickname
, see https://firebase.google.com/docs/firestore/query-data/get-data#get_a_document and https://firebase.google.com/docs/reference/js/firebase.firestore.DocumentSnapshot#data.
Also, you should return a result only once and you should not return outside of the .then()
if you want to return the result of the asynchronous operation.
In addition, see here how to handle errors: https://firebase.google.com/docs/functions/callable#handle_errors
So you may do as follows:
exports.storeContact5 = functions.https.onCall((data, context) => {
// First print is working fine
console.log('test');
var recieverId = 'WqHxLoYvRxR9UK8sFJZ9WxTOIE32';
const check = admin.firestore().collection('users').doc(recieverId).get();
return check.then(testValue => {
console.log(testValue.data().nickname);
return {repeat_message: 'ok!'};
}).catch(err => {
console.log('Error getting document', err);
throw new functions.https.HttpsError('Error getting document', err);
});
});
I would suggest you watch the videos from the official series: https://firebase.google.com/docs/functions/video-series/, in particular the ones titled "Learn JavaScript Promises"
Thanks a lot :)) All working now
– Ahmed Abosrie
Nov 20 '18 at 19:55
@AhmedAbosrie Welcome to StackOverflow. Glad that I could help! You may accept my answer by clicking on the grey checkmark next to the answer and turn it to green, see stackoverflow.com/help/someone-answers.
– Renaud Tarnec
Nov 20 '18 at 20:06
add a comment |
You should do testValue.data().nickname
and not testValue.data.nickname
, see https://firebase.google.com/docs/firestore/query-data/get-data#get_a_document and https://firebase.google.com/docs/reference/js/firebase.firestore.DocumentSnapshot#data.
Also, you should return a result only once and you should not return outside of the .then()
if you want to return the result of the asynchronous operation.
In addition, see here how to handle errors: https://firebase.google.com/docs/functions/callable#handle_errors
So you may do as follows:
exports.storeContact5 = functions.https.onCall((data, context) => {
// First print is working fine
console.log('test');
var recieverId = 'WqHxLoYvRxR9UK8sFJZ9WxTOIE32';
const check = admin.firestore().collection('users').doc(recieverId).get();
return check.then(testValue => {
console.log(testValue.data().nickname);
return {repeat_message: 'ok!'};
}).catch(err => {
console.log('Error getting document', err);
throw new functions.https.HttpsError('Error getting document', err);
});
});
I would suggest you watch the videos from the official series: https://firebase.google.com/docs/functions/video-series/, in particular the ones titled "Learn JavaScript Promises"
Thanks a lot :)) All working now
– Ahmed Abosrie
Nov 20 '18 at 19:55
@AhmedAbosrie Welcome to StackOverflow. Glad that I could help! You may accept my answer by clicking on the grey checkmark next to the answer and turn it to green, see stackoverflow.com/help/someone-answers.
– Renaud Tarnec
Nov 20 '18 at 20:06
add a comment |
You should do testValue.data().nickname
and not testValue.data.nickname
, see https://firebase.google.com/docs/firestore/query-data/get-data#get_a_document and https://firebase.google.com/docs/reference/js/firebase.firestore.DocumentSnapshot#data.
Also, you should return a result only once and you should not return outside of the .then()
if you want to return the result of the asynchronous operation.
In addition, see here how to handle errors: https://firebase.google.com/docs/functions/callable#handle_errors
So you may do as follows:
exports.storeContact5 = functions.https.onCall((data, context) => {
// First print is working fine
console.log('test');
var recieverId = 'WqHxLoYvRxR9UK8sFJZ9WxTOIE32';
const check = admin.firestore().collection('users').doc(recieverId).get();
return check.then(testValue => {
console.log(testValue.data().nickname);
return {repeat_message: 'ok!'};
}).catch(err => {
console.log('Error getting document', err);
throw new functions.https.HttpsError('Error getting document', err);
});
});
I would suggest you watch the videos from the official series: https://firebase.google.com/docs/functions/video-series/, in particular the ones titled "Learn JavaScript Promises"
You should do testValue.data().nickname
and not testValue.data.nickname
, see https://firebase.google.com/docs/firestore/query-data/get-data#get_a_document and https://firebase.google.com/docs/reference/js/firebase.firestore.DocumentSnapshot#data.
Also, you should return a result only once and you should not return outside of the .then()
if you want to return the result of the asynchronous operation.
In addition, see here how to handle errors: https://firebase.google.com/docs/functions/callable#handle_errors
So you may do as follows:
exports.storeContact5 = functions.https.onCall((data, context) => {
// First print is working fine
console.log('test');
var recieverId = 'WqHxLoYvRxR9UK8sFJZ9WxTOIE32';
const check = admin.firestore().collection('users').doc(recieverId).get();
return check.then(testValue => {
console.log(testValue.data().nickname);
return {repeat_message: 'ok!'};
}).catch(err => {
console.log('Error getting document', err);
throw new functions.https.HttpsError('Error getting document', err);
});
});
I would suggest you watch the videos from the official series: https://firebase.google.com/docs/functions/video-series/, in particular the ones titled "Learn JavaScript Promises"
edited Nov 20 '18 at 19:48
answered Nov 20 '18 at 19:32


Renaud TarnecRenaud Tarnec
10.8k21531
10.8k21531
Thanks a lot :)) All working now
– Ahmed Abosrie
Nov 20 '18 at 19:55
@AhmedAbosrie Welcome to StackOverflow. Glad that I could help! You may accept my answer by clicking on the grey checkmark next to the answer and turn it to green, see stackoverflow.com/help/someone-answers.
– Renaud Tarnec
Nov 20 '18 at 20:06
add a comment |
Thanks a lot :)) All working now
– Ahmed Abosrie
Nov 20 '18 at 19:55
@AhmedAbosrie Welcome to StackOverflow. Glad that I could help! You may accept my answer by clicking on the grey checkmark next to the answer and turn it to green, see stackoverflow.com/help/someone-answers.
– Renaud Tarnec
Nov 20 '18 at 20:06
Thanks a lot :)) All working now
– Ahmed Abosrie
Nov 20 '18 at 19:55
Thanks a lot :)) All working now
– Ahmed Abosrie
Nov 20 '18 at 19:55
@AhmedAbosrie Welcome to StackOverflow. Glad that I could help! You may accept my answer by clicking on the grey checkmark next to the answer and turn it to green, see stackoverflow.com/help/someone-answers.
– Renaud Tarnec
Nov 20 '18 at 20:06
@AhmedAbosrie Welcome to StackOverflow. Glad that I could help! You may accept my answer by clicking on the grey checkmark next to the answer and turn it to green, see stackoverflow.com/help/someone-answers.
– Renaud Tarnec
Nov 20 '18 at 20:06
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53399870%2fhow-to-get-data-from-firestore-in-cloud-function%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
@RahulMahadik - This is Cloud Function code!
– Ahmed Abosrie
Nov 20 '18 at 19:31