[英]How do you return a 'success' message to the frontend using onFinalize?
I am currently using a ReactJs frontend with the Firebase Javascript SDK.我目前正在使用带有 Firebase Javascript SDK 的 ReactJs 前端。 It is hooked up to a firebase-functions storage emulator using NodeJS in the backend.
它在后端使用 NodeJS 连接到 firebase-functions 存储模拟器。
Usually, I am able to call a functions.https.onCall(...)
cloud function (that has been set up with nodejs) from my frontend and receive a promise, and then I can use the data in the front end normally.通常,我可以从我的前端调用一个
functions.https.onCall(...)
云函数(已使用nodejs 设置)并收到一个promise,然后我就可以正常使用前端的数据了。
However, using functions.storage.object().onFinalize(...)
in the backend, I am trying to force some sort of way to return a success message to my front end after a file has successfully been processed.但是,在后端使用
functions.storage.object().onFinalize(...)
,我试图强制某种方式在成功处理文件后将成功消息返回到我的前端。
Here is what I've tried:这是我尝试过的:
My backend code (Nodejs):我的后端代码(Nodejs):
exports.processMedia = functions.storage.object().onFinalize(async (object) =>{
// Here I successfully process any uploaded images
// Issue - After the media has been processed, I'd like to return a message that can be used to update the UI. I've tried to return a new promise like so:
return new Promise((resolve, reject) =>{
return resolve("Success");
});
})
And then in my React UI, I am trying to capture the return message from the backend using a httpsCallable('processMedia')
function because I don't know how else to try to get the response.然后在我的 React UI 中,我尝试使用
httpsCallable('processMedia')
函数从后端捕获返回消息,因为我不知道如何尝试获取响应。
async processMedia(){
const processMediaCallable = this.functions.httpsCallable('processMedia');
return processMediaCallable;
}
Then I try to log out the values from the promise:然后我尝试从承诺中注销值:
async function getProcessedMessage(){
await firebase.processMedia().then((val) => {
console.log(val);
});
}
I get this output in the browser console for val
after I've uploaded a file in the UI and processMedia()
in the backend has completed:在 UI 中上传文件并且后端的
processMedia()
已完成后,我在val
的浏览器控制台中获得此输出:
ƒ (data) {
return _this.call(name, data, options || {});
}
I am trying to output a success message here.我正在尝试在此处输出成功消息。 Any thoughts would be greatly appreciated.
任何想法将不胜感激。 Thank you.
谢谢你。
You cannot return a response to client from a background function.您不能从后台函数向客户端返回响应。 When a user uploads any file to your Firebase storage they get a success response and the function triggered from that event has nothing to do with client.
当用户将任何文件上传到您的 Firebase 存储时,他们会收到成功响应,并且该事件触发的函数与客户端无关。 There's a workaround for using Firebase Realtime Database which you can use to emit the response to user.
有一种使用Firebase 实时数据库的解决方法,您可以使用它向用户发出响应。
To break it down in simple steps:用简单的步骤分解它:
Client:客户:
async function uploadFile() {
// upload to firebase storage
const alertRef = firebase.database().ref(`alerts/${userID}`);
alertRef.on('child_added', (snapshot) => {
const data = snapshot.val();
// Alert received from cloud function
// Delete this alert from database
snapshot.ref.set(null)
});
}
Cloud function:云功能:
exports.processMedia = functions.storage.object().onFinalize(async (object) => {
// Process the file
// Add alert in realtime database
const alertRef = firebase.database().ref(`alerts/${userID}`).push()
return alertRef.set({...}) // add any data you would like to pass to client side
})
child_added
event is triggered once for each existing child and then again every time a new child is added to the specified path.child_added
事件为每个现有子项触发一次,然后在每次将新子项添加到指定路径时再次触发。 The listener is passed a snapshot containing the new child's data.侦听器被传递一个包含新孩子数据的快照。
That being said just make sure you are not alerting users multiple times with same notification.话虽如此,但请确保您没有用相同的通知多次提醒用户。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.