Ahm*_*rie 2 firebase google-cloud-functions google-cloud-firestore
我需要知道我在这里做错了什么?
我正在从 Flutter 调用这个函数。回调已正确完成,第一次和第二次打印将出现在 Firbase 的“日志”中。但是从“Firestore”中获取未定义!!
这是云函数中的代码:
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!'
}
});
Run Code Online (Sandbox Code Playgroud)
Firebase 日志的屏幕截图
你应该做testValue.data().nickname而不是testValue.data.nickname,请参阅https://firebase.google.com/docs/firestore/query-data/get-data#get_a_document和https://firebase.google.com/docs/reference/js/firebase.firestore .DocumentSnapshot#data。
此外,.then()如果您想返回异步操作的结果,您应该只返回一次结果,并且不应在 之外返回。
此外,请参阅此处如何处理错误:https : //firebase.google.com/docs/functions/callable#handle_errors
所以你可以这样做:
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);
});
});
Run Code Online (Sandbox Code Playgroud)
我建议您观看官方系列中的视频:https : //firebase.google.com/docs/functions/video-series/,尤其是标题为“Learn JavaScript Promises”的视频
| 归档时间: |
|
| 查看次数: |
3265 次 |
| 最近记录: |