NewCEO
NewCEO

Reputation: 51

Firebase Function returned undefined, expected Promise or value but the function works

Hi I have a function that works but gives me an error of Function returned undefined, expected Promise or value in the middle. I do not understand why. The functions works though. Please help

Thanks in advance..

enter image description here

here is the function

'use-strict'

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

 exports.deleteFriendChatMessagesImageFolder = 
 functions.database.ref('/messagesFriends/{userId}/{friendId}')
            .onDelete((snap, context) => {

 const userId = context.params.userId;
 const friendId = context.params.friendId;

 const bucket = firebase.storage().bucket();

 console.log(userId + ' ' + friendId + " found")

 return bucket.deleteFiles({
    prefix: `messages_image_from_friends/`+userId+`/`+friendId
    }, function(err) {
        if (err) {
   console.log(`messages_image_from_friends/`+userId+`/`+friendId + ` remove error`);
            } else {
   console.log(`messages_image_from_friends/`+userId+`/`+friendId + ` removed`);
                }
                  });

 });

Where should i put the promise to make the error go away. Again the function works! So i am confused.

Upvotes: 0

Views: 44

Answers (1)

Louis Coulet
Louis Coulet

Reputation: 4591

When you provide a callback to deleteFiles, it does not return anything (source).
This is a common pattern with JS APIs: either use a callback or return a promise.
Here is how you could modify your code to use promises:

 return bucket.deleteFiles({
        prefix: `messages_image_from_friends/`+userId+`/`+friendId
    })
    .then(() => {
        console.log(`messages_image_from_friends/`+userId+`/`+friendId + ` removed`)
    })
    .catch((err) => {
        console.log(`messages_image_from_friends/`+userId+`/`+friendId + ` remove error`)
    });

A good place to learn about promises is here.

Upvotes: 2

Related Questions