Kelvin Zhao
Kelvin Zhao

Reputation: 2425

Firebase cloud functions - Function returned undefined, expected Promise or value

I've read thru the other threads and tried to return something to my cloud functions, but it still gives the same Function returned undefined, expected Promise or value warning... What am I missing here?

Basically, I'm sending two emails out using mailgun-js to the user and to the admin, even though the sending is successful and stuff, I still get the same warning in my logs.

exports.sendSample = functions.database
.ref( 'sample/{pushID}' )
.onWrite( event => {

    //  only trigger if it's new
    if ( !event.data.exists() || event.data.previous.exists() ) return 0

    const e = event.data.val()

    const toUser = {
        to      : `${ e.email }`,
        from    : `xxx`,
        subject : `xxx`,
        html    : `xxx`
    }

    const toAdmin = {
        to      : `${ e.admin }`,
        from    : `xxx`,
        subject : `xxx`,
        html    : `xxx`
    }

    //  send sms
    if ( e.mobile !== 'none' ) {
        return client.messages.create({
            body : `xxx`,
            to   : `${e.mobile}`,
            from : `xxx`
        })
        .then( message => {
            sendEmail()
            return message.sid
        })
        .catch( err => {
            return err
        })
    } else {
        sendEmail()
    }

    //  send email
    function sendEmail() {
        if ( e.email !== 'none' ) {
            return mailgun.messages().send( toUser, ( err, body ) => {
                return ( err, body )
                return mailgun.messages().send( toAdmin, ( err, body ) => {
                    return ( err, body )
                })
            })
        } else {
            return true
        }
    }
})

Upvotes: 0

Views: 2642

Answers (2)

Akisazaki RIn
Akisazaki RIn

Reputation: 105

How about this ?

// send sms
if ( e.mobile !== 'none' ) {
  ...

} else {
    sendEmail()
}

to

// send sms
if ( e.mobile !== 'none' ) {
  ...

} else {
    return sendEmail()
}

Upvotes: 0

4face
4face

Reputation: 589

As already been said, you need to return a value.

if (e.email !== 'none') {
    ...
} else {
    return true;
}

Upvotes: 2

Related Questions