【发布时间】:2016-12-27 17:41:21
【问题描述】:
我有一个 angularjs 仪表板,用户应该在其中填写表格(提供 ID 和日期),然后将此数据发送到 express/nodejs 服务器,该服务器对 mongodb 进行查询。
我的问题是如果用户填写了数据库中不存在的ID,mongodb会返回错误"cannot read property count of undefined",快递服务器会崩溃。
我尝试使用 try/catch 处理错误,但没有任何变化。
try {
collection
.aggregate(
[{$match: {"content.MemberID_Hash": "xxx", "content.Swipe_DateTime": {$regex:"201602"}}},
{$group: {_id: null, count:{$sum: 1}}}],
function(err,result) {
console.log("User's number of swipes from last month: ", result[0].count);
//findUserRank(result[0].count);
//getNeighbouringValues(result[0].count);
});
} catch (err) {console.log("Wrong query"); console.error(err);}
有没有办法在保持服务器运行的同时返回错误?
【问题讨论】: