node.js - 如果不存在任何值,我如何捕捉承诺错误?
问题描述
当用户输入促销代码时,这可能存在也可能不存在。我以这种方式返回错误,但似乎服务器崩溃并返回许多错误行。如何优化代码以使其更干净?这个表格正确吗?我对节点和 JS 的控制不多,很难看出哪个是最好的方法......
这是我在控制器中的代码:
var mongoose = require('mongoose'),
Promotion = mongoose.model('Promotion'),
q = require('q'),
Promise = q.Promise;
var PromotionController = {
applyPromo: function(promoCode) {
return new Promise(function(resolve, reject) {
if(promoCode === null) {
resolve(false);
}
Promotion.find({code: promoCode}, function (err, elements) {
if (err)
{
reject(err);
}
if((elements !== null) && (elements.length > 0)) {
console.log('OK');
console.log(elements[0]);
resolve(elements[0]._id);
}
else {
//reject(new Error('Doesnt exist ' + promoCode));
}
});
});
}
};
module.exports = PromotionController;
我的服务器显示:
Error: Doesnt exist codeExample
at /home/user/Documentos/myApi/myServer/api/controllers/promotions.js:36:13
at /home/user/Documentos/myApi/myServer/node_modules/mongoose/lib/model.js:4599:16
at /home/user/Documentos/myApi/myServer/node_modules/mongoose/lib/query.js:4122:12
at cb (/home/user/Documentos/myApi/myServer/node_modules/mongoose/lib/query.js:1777:14)
at result (/home/user/Documentos/myApi/myServer/node_modules/mongodb/lib/utils.js:414:17)
at executeCallback (/home/user/Documentos/myApi/myServer/node_modules/mongodb/lib/utils.js:406:9)
at handleCallback (/home/user/Documentos/myApi/myServer/node_modules/mongodb/lib/utils.js:128:55)
at cursor.close (/home/user/Documentos/myApi/myServer/node_modules/mongodb/lib/operations/cursor_ops.js:224:62)
at handleCallback (/home/user/Documentos/myApi/myServer/node_modules/mongodb/lib/utils.js:128:55)
at completeClose (/home/user/Documentos/myApi/myServer/node_modules/mongodb/lib/cursor.js:893:14)
at Cursor.close (/home/user/Documentos/myApi/myServer/node_modules/mongodb/lib/cursor.js:912:10)
at cursor._next (/home/user/Documentos/myApi/myServer/node_modules/mongodb/lib/operations/cursor_ops.js:224:23)
at handleCallback (/home/user/Documentos/myApi/myServer/node_modules/mongodb-core/lib/cursor.js:204:5)
at _setCursorNotifiedImpl (/home/user/Documentos/myApi/myServer/node_modules/mongodb-core/lib/cursor.js:433:38)
at self._endSession (/home/user/Documentos/myApi/myServer/node_modules/mongodb-core/lib/cursor.js:441:46)
at Cursor._endSession (/home/user/Documentos/myApi/myServer/node_modules/mongodb-core/lib/cursor.js:195:5)
at Cursor._endSession (/home/user/Documentos/myApi/myServer/node_modules/mongodb/lib/cursor.js:226:59)
at _setCursorNotifiedImpl (/home/user/Documentos/myApi/myServer/node_modules/mongodb-core/lib/cursor.js:441:17)
at setCursorNotified (/home/user/Documentos/myApi/myServer/node_modules/mongodb-core/lib/cursor.js:433:3)
at done (/home/user/Documentos/myApi/myServer/node_modules/mongodb-core/lib/cursor.js:648:16)
at queryCallback (/home/user/Documentos/myApi/myServer/node_modules/mongodb-core/lib/cursor.js:699:18)
at /home/user/Documentos/myApi/myServer/node_modules/mongodb-core/lib/connection/pool.js:532:18
at process._tickCallback (internal/process/next_tick.js:61:11)
解决方案
推荐阅读
- javascript - 如何导入本地js包而不是网络包
- wix - 在 Bundle 中设置 InstallPrivileges = "elevated"。引导程序蜡
- html - 如何以角度为输入列表属性分配动态值
- java - 如何在 JAVA 中的 SSL 握手期间将签名算法从一个提供者添加到另一个提供者
- ms-access - 表未使用 runsql 保存在 Access 中
- spring - 如何控制 Flux.flatMap (Mono) 的并行性?
- c# - 统一空中运动与速度变化
- maven - 与原始工件相同的 POM 父工件 ID 是
- reactjs - 如何添加图像 src 以响应状态和设置状态
- random - 在已知边界的图中查找随机 x,y 坐标