如何将数据从 angular 传递到我的云函数并获得响应?
How can I pass data from angular to my cloud function and get a response back?
目前,我尝试将数据从我的 angular 前端传递到云函数,并希望接收一个布尔值,如果该过程成功则该值变为真。
我的index.ts文件:
export const someTesting = functions.https.onCall(async (data) => {
let success = false;
const testData = {...data};
await admin.firestore().collection('collection').add(testData).then(() => {
return success = true;
});
});
我的angular.ts文件
async callCloudFunction(testData: TestData) {
const sendDataToCloudFunctions = this.fun.httpsCallable('someTesting');
// here I pass the data to the cloud function
sendDataToCloudFunctions(testData);
// here I want to consolte.log() the boolean success ...
}
如果有人能告诉我如何做到这一点,那就太好了。谢谢!
Callable type Cloud Functions 需要 return 一个使用数据对象解析的承诺,以发送给客户端。现在,您的功能 return 什么都没有。您拥有的一个 return 语句只是 return 来自您传递给 then
.
的回调函数
您也不应该将 async/await 语法与 then 混合使用。只用其中一个,因为它们具有相同的用途。
如果您只想return数据库操作完成后的布尔成功状态,那么请执行以下操作:
export const someTesting = functions.https.onCall(async (data) => {
let success = false;
const testData = {...data};
await admin.firestore().collection('collection').add(testData);
return { success: true };
});
要在您的应用中获得结果,您需要像处理任何其他承诺一样处理由 sendDataToCloudFunctions
编辑的承诺 return。
目前,我尝试将数据从我的 angular 前端传递到云函数,并希望接收一个布尔值,如果该过程成功则该值变为真。
我的index.ts文件:
export const someTesting = functions.https.onCall(async (data) => {
let success = false;
const testData = {...data};
await admin.firestore().collection('collection').add(testData).then(() => {
return success = true;
});
});
我的angular.ts文件
async callCloudFunction(testData: TestData) {
const sendDataToCloudFunctions = this.fun.httpsCallable('someTesting');
// here I pass the data to the cloud function
sendDataToCloudFunctions(testData);
// here I want to consolte.log() the boolean success ...
}
如果有人能告诉我如何做到这一点,那就太好了。谢谢!
Callable type Cloud Functions 需要 return 一个使用数据对象解析的承诺,以发送给客户端。现在,您的功能 return 什么都没有。您拥有的一个 return 语句只是 return 来自您传递给 then
.
您也不应该将 async/await 语法与 then 混合使用。只用其中一个,因为它们具有相同的用途。
如果您只想return数据库操作完成后的布尔成功状态,那么请执行以下操作:
export const someTesting = functions.https.onCall(async (data) => {
let success = false;
const testData = {...data};
await admin.firestore().collection('collection').add(testData);
return { success: true };
});
要在您的应用中获得结果,您需要像处理任何其他承诺一样处理由 sendDataToCloudFunctions
编辑的承诺 return。