【问题标题】:"A promise was created in a handler but was not returned from it"“在处理程序中创建了一个承诺,但没有从它返回”
【发布时间】:2017-01-04 10:49:33
【问题描述】:

当用户单击按钮 (#lfdsubmit) 时,它会调用应返回承诺的函数 (LFD_SearchContainer())。但是错误发生在

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

怎么了? 代码:(不要完全相信注释掉的部分会引导你完成这段代码——我忘了​​更新其中的一些)

函数 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);
        });
    });
});

}

【问题讨论】:

    标签: javascript promise bluebird


    【解决方案1】:

    如果ST2.db2(container); 返回一个promise,则需要将该行更改为

    return ST2.db2(container);
    

    如果没有,你可以把return null;放在后面,像这样:

    ST2.db2(container);
    return null;
    

    由于您没有提供ST2 的定义,我不知道db2 方法是否返回一个promise。 :)

    这个错误由​​bluebirdhere的作者解释。

    【讨论】:

    • 返回“null”解决了这个问题。谢谢
    • @Chris:请接受我的回答,这样这个问题就解决了:)
    猜你喜欢
    • 2016-03-01
    • 2016-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-02
    • 1970-01-01
    • 2016-09-20
    • 2016-08-23
    相关资源
    最近更新 更多