Search code examples
javascriptpromisebluebird

"A promise was created in a handler but was not returned from it"


When a user clicks on a button (#lfdsubmit), it calls the function (LFD_SearchContainer()) that should return a promise. But the errors happens at

LFD_SearchContainer('EISU1870725')
.then(container => {
  ST2.db2(container);
})

What is wrong? Code: (don't completely trust the commented out parts to guide you through this code -- i forgot to update some of them)

function LFDTrack () {

function LFD_SearchContainer (requestedContainer) {
    return new Promise((resolve, reject) => {
        let lfd_scanparams = { TableName: 'lfd_table1' }
        db.scan(lfd_scanparams, (err, containers) => {
            if (err) {
                reject(err);
            } else {
                containers = containers.Items;

                let requestedContainers = []; // different variable than arg

                let containerObject; // this will be the resolved object

                // this will return the object of the searched container
                let findIt = _.forEach(containers, container => {
                    if (container.container === requestedContainer) {
                        containerObject = container;
                    }
                });
                containerObject = findIt[0];
                //console.log(findIt[0]);
                resolve(containerObject.container);
            }
        });
    });
}

$(function() {
    $("#lfdsubmit").click(function (e) {
        e.preventDefault();

        let lsd_modaltitle = $("#lfdmodaltitle");
        let lsd_modalcontent = $("#lfdmodalcontent");

        LFD_SearchContainer('EISU1870725')
        .then(container => { 
            ST2.db2(container); // will send the object
        })
        .catch(error => {
            console.log(error);
        });
    });
});

}


Solution

  • If ST2.db2(container); returns a promise, you need to change that line to

    return ST2.db2(container);
    

    If it doesn't, you can put return null; behind it, like this:

    ST2.db2(container);
    return null;
    

    Since you didn't provide the definition for ST2, I can't know whether or not the db2 method returns a promise. :)

    The error is explained by the author of bluebird here.