node.js (节点:2684)UnhandledPromiseRejectionWarning:未处理的承诺拒绝(拒绝ID:1):TypeError:无法读取未定义的属性“then”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42395021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 18:02:09  来源:igfitidea点击:

(node:2684) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: Cannot read property 'then' of undefined

node.jsexpresspromise

提问by Harsimer

I am using .then for first time, instead of .then I use callback function.

我第一次使用 .then,而不是 .then 我使用回调函数。

Below is my code snippet:

下面是我的代码片段:

phantom.create().then(function (ph) {
    ph.createPage().then(function (page) {
        page.open("http://codebeautify.org/xmlvalidator").then(function (status) {
            page.render(base_pdf_path + lpnumber + '.pdf').then(function () {
                console.log('PDF generated for waybill');
                //Insert waybill url in db.
                return waybill_url.insertWaybillUrl('Lpsimer', waybillUrl).then(function (waybill_inserted_resp) {
                    callback(null, true);   
                }).catch(function (error) {
                    callback(err_waybill_inserted);
                });
            });
        });
    });
});

The above function is calling a function which is as below, this is in another file and called properly filename is waybill.js:

上面的函数正在调用一个如下的函数,这是在另一个文件中并且正确调用文件名是waybill.js:

var mongoose = require('mongoose');
var q = require('promised-io/promise');

var WaybillUrlSchema = new mongoose.Schema({
    lpnumber: String,
    url: String,
    waybilltime: Date
});

module.exports = {

    insertWaybillUrl: function (lpnumber, url) {
        var defer = q.defer();
        var waybill_insert = new waybill_url({
            lpnumber: lpnumber,
            url: url,
            waybilltime: new Date()
        });

        //Save model to MongoDB
        waybill_insert.save(function (err, inserted_waybill) {
            if (err) {
                return defer.reject(err);
            }
            else {
                return defer.resolve(inserted_waybill);
            }
        });
    }
};

Previously I was using this pattern to make callbacks and it was working fine:

以前我使用这种模式进行回调,它工作正常:

waybill_url.insertWaybillUrl('Lpsimer', waybillUrl, function(err, success) {
   if (err) {

   } else {

   }
)}

Now I have to use .then due to usage of phantom code to write PDF and it has made the job cumbersome.

现在我必须使用 .then 由于使用幻影代码来编写 PDF 并且它使工作变得繁琐。

Need suggestion on how I can make callbacks within callbacks.

需要关于如何在回调中进行回调的建议。

UPDATE

更新

phantom.create().then(function (ph) {

    ph.createPage().then(function (page) {

        page.open("http://codebeautify.org/xmlvalidator").then(function (status) {
            page.render(base_pdf_path + lpnumber + '.pdf').then(function () {

                //Insert waybill url in db.

                waybill_url.insertWaybillUrl('Lpsimer', waybillUrl).then(function (waybill_inserted_resp) {

                    if (waybill_inserted_resp) {

                        callback(null, true);

                    }

                }).catch(function (error_waybill_url_insert) {

                    console.log("Error in inserting waybill:" + err_waybill_inserted);

                    callback(error_waybill_url_insert);
                });

            }).catch(function (error_render) {

                console.log("error_render");
                callback(error_render);
            });

        }).catch(function (error_open) {

            callback(error_open);

        });

    }).catch(function (error_create_page) {

        callback(error_create_page);

    });

}).catch(function (error_phantom_create) {

    callback(error_phantom_create);
});

Now I have added catch for every then as suggested by rsp in his answer, but now I am getting error which I have catched and send to another callback:

现在我按照 rsp 在他的回答中的建议为每个 then 添加了 catch ,但是现在我收到错误,我已经捕获并发送到另一个回调:

Cannot read property 'then' of undefined

无法读取未定义的属性“then”

I am getting this error where I have added console.log("error_render"); that is where I am calling page.render function of phantom.

我在添加 console.log("error_render"); 的地方遇到了这个错误。那就是我调用phantom的page.render函数的地方。

My requirement is simple. I want to generate a PDF file using phantom and then I just want to call another function which is in another file waybill_url, function name: waybill_url.insertWaybillUrl. But due to callbacks and asynchronous behaviour this simple calling of two functions is getting cumbersome.

我的要求很简单。我想使用phantom生成一个PDF文件,然后我只想调用另一个文件waybill_url中的另一个函数函数名称:waybill_url.insertWaybillUrl。但是由于回调和异步行为,这两个函数的简单调用变得很麻烦。

回答by rsp

Make sure that you use catch()and not only then()- or two arguments to then(). Otherwise you will not handle errors and you will get that warning - which will be an error, not a warning, in next versions of Node.

确保您使用catch()and 而不仅仅是then()- 或两个参数到then(). 否则,您将无法处理错误,并且会收到该警告 - 在 Node.js 的下一个版本中,这将是错误,而不是警告。

See this answer for more info about it:

有关它的更多信息,请参阅此答案: