Rahul
Rahul

Reputation: 131

Function returned undefined, expected Promise or value

Whern i fire Simple database trigger on firebase, then it will shows Error like this : "Function returned undefined, expected Promise or value"

const firebase=require('firebase-admin');

const functions = require('firebase-functions');
firebase.initializeApp(functions.config().firebase);

exports.helloNotification = functions.database.ref('/users').onWrite(event => {

 return "A Notification has been deleted from the database ";
});

Upvotes: 1

Views: 749

Answers (3)

Darshit Anjaria
Darshit Anjaria

Reputation: 101

make some changes

instead of return statement write console.log('...');

add 'use strict' at the top of the code

write admin instead of firebase to initialize the app

the code looks like

'use strict'
const firebase=require('firebase-admin');

const functions = require('firebase-functions');
admin.initializeApp(functions.config().firebase);

exports.helloNotification = functions.database.ref('/users').onWrite(event => {

 console.log('A Notification has been deleted from the database');
});

Upvotes: 0

Doug Stevenson
Doug Stevenson

Reputation: 317808

If you don't have any asynchronous work to do in a function, just return null. Returning a string doesn't have any meaning in Cloud Functions. If you need to return a promise for some reason, just return Promise.resolve().

Upvotes: 4

Troels Lenda
Troels Lenda

Reputation: 524

If you just want the error to disappear you could simply just

return new Promise((resolve, reject) => {
    resolve("A Notification has been deleted from the database ")
})

but it would be kinda pointless to return a string here.

If it's just for testing you could

console.log("A Notification has been deleted from the database ")

instead.

Upvotes: 1

Related Questions