Javascript 如何将数据从firestore获取到google云功能?
我的index.js文件:Javascript 如何将数据从firestore获取到google云功能?,javascript,firebase,flutter,google-cloud-firestore,google-cloud-functions,Javascript,Firebase,Flutter,Google Cloud Firestore,Google Cloud Functions,我的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();
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");
})
});
颤振项目中的代码:
HttpsCallable callable = FirebaseFunctions.instance.httpsCallable("getName");
var temp = await callable({"id": "11"});
print(temp.data);
即使集合“dogs”中存在id为“11”且字段名为的文档,程序也会输出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;
});
您是否尝试过使云函数异步
exports.getName = functions.https.onCall(async (data, context) => {
var doc = await db.collection("dogs").doc("{data.id}").get();
return doc.data().name;
});
答案是正确的,但让我用then()
解释为什么它不适用于初始代码
通过这样做:
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");
})
});
实际上,您不会返回doc.get(“name”)代码>在可调用的云函数中。如中所述,then()
方法确实返回了Promise.resolve(doc.get(“name”))
,但不返回
以下工作将起作用:
exports.getName = functions.https.onCall((data, context) => {
var docRef = db.collection("dogs").doc("{data.id}");
return docRef.get().then(doc => {
return doc.get("name");
})
});
顺便说一句,您确定db.collection(“dogs”).doc(“{data.id}”)代码>是否正确?它不应该是db.collection(“dogs”).doc(data.id)代码>?答案是正确的,但让我用then()
解释为什么它不适用于初始代码
通过这样做:
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");
})
});
实际上,您不会返回doc.get(“name”)代码>在可调用的云函数中。如中所述,then()
方法确实返回了Promise.resolve(doc.get(“name”))
,但不返回
以下工作将起作用:
exports.getName = functions.https.onCall((data, context) => {
var docRef = db.collection("dogs").doc("{data.id}");
return docRef.get().then(doc => {
return doc.get("name");
})
});
顺便说一句,您确定db.collection(“dogs”).doc(“{data.id}”)代码>是否正确?它不应该是db.collection(“dogs”).doc(data.id)代码>