1

I want to organize sending push- notifications on adding a document to firestore. I am using the code from examples from firebase site for node.js.

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
var message = {
    notification: {
        title: 'title!',
        body: 'body'
    },
    topic: "all"
};


exports.createRequest = functions.firestore
    .document('Requests/{RequestsId}')
    .onCreate((snap, context) => {
        console.log('We have a new request');
        // Send a message to devices subscribed to the provided topic.
        admin.messaging().send(message)
            .then((response) => {
                console.log('Successfully sent message:', response);
            }).catch((error) => {
                console.log('Error sending message:', error);
            });
        return 0;
    });

When I try to deploy I am getting an error:

Each then() should return a value or throw promise/always-return" for string .then((response) => {

1
  • Please take more consideration in formatting your code on Stack Overflow. Use the {} button in the editor to format code sections. Markdown doesn't really work the way you expect. Commented May 19, 2019 at 6:19

1 Answer 1

1

Change this:

admin.messaging().send(message)
.then((response) => {
  console.log('Successfully sent message:', response);
}).catch((error) => {
  console.log('Error sending message:', error);
  });
 return 0;
 });

Into this:

return admin.messaging().send(message)
.then((response) => {
 console.log('Successfully sent message:', response);
 return null;
}).catch((error) => {
 console.log('Error sending message:', error);
  });
});

You need to terminate functions correctly, so you can avoid excessive charges from functions that run for too long or loop infinitely.

You can use the following ways to terminate your function:

Resolve functions that perform asynchronous processing (also known as "background functions") by returning a JavaScript promise.

Terminate HTTP functions with res.redirect(), res.send(), or res.end().

Terminate a synchronous function with a return; statement.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.