我们有一个带有 firestore 数据库的应用,使用 firebase 云功能。我们正在尝试从外部 API 获取有关每个用户的数据。我们的 firebase 云函数正在返回数据 - 我可以在日志中正确地看到它。但是,我在浏览器中看不到该数据。我猜也许我没有正确使用 async/await?
以下是我们如何从我们的应用程序(在 Vuex 中)调用该函数:
async retrieveByExternalId({ commit }, payload) {
const retrieveByExternalId = await firebase.functions().httpsCallable('retrieveByExternalId')
retrieveByExternalId({
id: payload
})
.then(result => {
console.log(result.data)
commit('setUserContractorPayProfile', result.data)
})
},
Result.data 显示为 null
然后,这是云函数:
exports.retrieveByExternalId = functions.https.onCall(async (data, context) => {
const id = data.id
axios({
method: "GET",
url: `https://website/api/v2/workers/external/${id}`,
headers: {
accept: '*',
'Access-Control-Allow-Origin': '*',
Authorization: 'API KEY'
}
})
.then(response => {
functions.logger.log("Response", " => ", response.data);
return response.data
})
.catch((error) => {
functions.logger.log("Error", " => ", error);
})
});
在函数日志中,我可以正确地看到所有内容。
这是一个异步/等待问题吗?或者我返回的数据错误?
谢谢!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
我还没有尝试过你的代码,但问题很可能是由于你没有返回 云函数中的 Promise 链。
你应该这样做:
return axios({ // <====== See return here // ... }) .then(response => { functions.logger.log("Response", " => ", response.data); return response.data }) .catch((error) => { functions.logger.log("Error", " => ", error); })或者,由于您声明了函数
async,请使用await关键字,如下所示:exports.retrieveByExternalId = functions.https.onCall(async (data, context) => { try { const id = data.id const axiosResponse = await axios({ method: "GET", url: `https://website/api/v2/workers/external/${id}`, headers: { accept: '*', 'Access-Control-Allow-Origin': '*', Authorization: 'API KEY' } }); functions.logger.log("Response", " => ", axiosResponse.data); return axiosResponse.data } catch (error) { // see https://firebase.google.com/docs/functions/callable#handle_errors } });