gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 03:27:47 25 4
gpt4 key购买 nike

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

下面是我的代码片段:

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

上面的函数正在调用如下函数,该函数在另一个文件中,正确调用的文件名是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);
}
});
}
};

之前我使用此模式进行回调,并且运行良好:

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

} else {

}
)}

现在我必须使用.then,因为使用幻像代码来编写PDF,这使得工作变得很麻烦。

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

更新

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

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

Cannot read property 'then' of undefined

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

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

最佳答案

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

有关详细信息,请参阅此答案:

关于node.js - ( Node :2684) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: Cannot read property 'then' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42395021/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com