我使用count方法按条件计算记录总数。这是我的代码:
Users.count({
$or: [
{ username : { "$regex": search } },
{ email : { "$regex": search } },
{ lastName : { "$regex": search } },
{ firstName : { "$regex": search } }
]
}).exec(function (err, total) {
console.log(total);
return res.json({
"sEcho": 0,
"iTotalRecords": total,
"iTotalDisplayRecords": 7,
"aaData": users
});
});
我总是收到“total”为 0 值以及“search”参数的任何值。 我的错误是什么以及如何解决?
非常感谢!
最佳答案
试试这个:
Users.count({
or: [
{ username : { contains: search } },
{ email : { contains: search } },
{ lastName : { contains: search } },
{ firstName : { contains: search } }
]
}).exec(function (err, total) {
console.log(total);
return res.json({
"sEcho": 0,
"iTotalRecords": total,
"iTotalDisplayRecords": 7,
"aaData": users
});
});
关于node.js - SailsJS 模型按条件计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32687169/