-2

I have firebase project that has few cloud functions. Some of them sends push notifications to the users. Before deploying any function, I develop it using the nodeJS on my local environment with standalone nodejs project.

However today I noticed that the function execution gets stuck after I try to send a push notification to even a single user. I mean to say the cursor does not return after I hit the node test.js and the execution never stops. I need to do ctrl + z in order to end the execution. However I get the push notification.

My code:

async function sendTest() {
   await admin.messaging().sendToDevice(token, payload);
   console.debug('Message Sent'); // this line gets printed instantly but execution continues
}

The following is also a similar code but it also doesn't end the execution even after printing the debug. statement.

async function sendTest() {
   var notificationPersonalPromises = []
   notificationPersonalPromises.push(admin.messaging().sendToDevice(token, payload));
   await Promise.all(notificationPersonalPromises);
   console.debug('Message Sent'); // this line gets printed instantly but execution continues
} 

In both the above cases, I get the notification in couple of seconds and the debug gets printed instantly. If I comment out the push notification code then everything works fine. Please help...

The following image shows how the functions never ends the cursor keeps waiting... enter image description here

Thanks

Sam
  • 2,972
  • 6
  • 34
  • 62
  • Can you show the cloud function code fully used here not just the function used for better understanding. – Haris Wilson Aug 28 '21 at 06:31
  • Hi, Sorry for the delay. I just added the code image. This is the only function I am calling. – Sam Aug 30 '21 at 15:14

1 Answers1

0

Make sure that your cloud function have a retunr. Even if you don't return anything. You can also return your last promise like:

async function sendTest() {
   return admin.messaging().sendToDevice(token, payload);
}

or just an empty return like here:

async function sendTest() {
   await admin.messaging().sendToDevice(token, payload);
   console.debug('Message Sent'); // this line gets printed instantly but execution continues
  return
}
Tarik Huber
  • 7,061
  • 2
  • 12
  • 18
  • I added the return statement after the console.debug but still it is not ending... – Sam Aug 27 '21 at 00:30
  • 1
    How do you check if the functions is still runing? – Tarik Huber Aug 27 '21 at 12:11
  • I added an image that shows how the cursor never returns even after printing the last console.debug line just before the return. – Sam Aug 28 '21 at 00:55
  • You have the code in a if statement. Could you pls share all of your code with us. Maybe there is something above making the problem. – Tarik Huber Aug 28 '21 at 06:48
  • Even if I remove the else part it stuck there only. I found another thread like this https://stackoverflow.com/questions/21831493/my-nodejs-script-is-not-exiting-on-its-own-after-successful-execution. but I don't know how can I do process.exit() and return the value if there is any – Sam Aug 29 '21 at 00:26
  • We can't help you if you refude to share your code. As explained in the link you shared they might be listeners, handles etc.. that you start before that code and that may interrupt thex exit of the process. That is the reason I ask for the code before. – Tarik Huber Aug 29 '21 at 08:11
  • Hi, Sorry for the delay. I just added the code image. Demo is the only function I am calling. – Sam Aug 30 '21 at 15:14
  • I don't see any connection between the first code and the one you added. But try to add and `await` for the sending and also try to add `process.exit()` before the return. – Tarik Huber Aug 30 '21 at 20:14
  • when I add the process.exti() it works but what if I want to return any value from this function ? Also I am worried about the Firebase functions... will I have to keep the process.exit() there also? – Sam Aug 31 '21 at 00:18
  • 1
    You need the `process.exit()` only on the total end of your code and not in each function. It would exit your whole app. You also don't need it in the cloud functions. They accept a normal `return` as exit point. That was the reason why I asked you how you test your code and solution. The cloud functions are running in a different environment and as such behave slightliy different than your local tests. – Tarik Huber Aug 31 '21 at 06:48