我的index.js文件:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const Firestore = require('@google-cloud/firestore');
const firestore = new Firestore();
admin.initializeApp();
const db = admin.firestore();
exports.getName = functions.https.onCall((data, context) => {
var docRef = db.collection("dogs").doc("{data.id}");
var getDoc = docRef.get().then(doc => {
return doc.get("name");
})
});
flutter项目中的代码:
HttpsCallable callable = FirebaseFunctions.instance.httpsCallable("getName");
var temp = await callable({"id": "11"});
print(temp.data);
即使存在 ID 为“11”、字段名称为“dogs”的集合中的文档,程序也会打印出 null。我正在尝试从 firestore 获取特定数据并将其返回。
控制台不会显示任何错误,如果我返回任何其他内容,它会正常打印出来。
除了使用 onWrite 等触发器的文档之外,找不到任何有关从 Firestore 获取数据到云函数的文档。
最佳答案
您是否尝试过使云函数异步?
exports.getName = functions.https.onCall(async (data, context) => {
var doc = await db.collection("dogs").doc("{data.id}").get();
return doc.data().name;
});
关于javascript - 如何从firestore获取数据到谷歌云功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66013087/