'the limit must be specified as a number' 使用 req.query.property 时出错
'the limit must be specified as a number' error when using req.query.property
我正在管道中使用 $limit
执行 Mongoose/MongoDB .aggregate
查询。当我使用数字时,如 2
,它工作正常。如果我设置一个像 testNum = 2
这样的变量,然后执行 {$limit: varNum}
,它工作正常。但是,如果我发送一个 REST 查询并尝试执行 $limit: req.body.show
,它会说该值不是数字。
通过console.log
可以看出是一个数字。管道中的其他查询不会抱怨没有给出数字。这是代码:
var show = req.query.show, // the number of items to show per page
page = req.query.page, // the current page being asked for
stream = req.params.stream, // the type of content to get
skip = ( page > 0 ? (( page - 1 ) * show ) : 0 ), // amount to skip
testNum = 3
console.log( show + " " + skip + " " + page )
Content.aggregate( [
{ $unwind: '$users' },
{ $group: {
_id: '$_id',
title: { $first: '$title' },
description: { $first: '$description' },
images: { $first: '$images' },
url: { $first: '$url' },
saveCount: { $sum: 1 } }
},
{ $sort: { saveCount: -1 } },
{ $skip: skip },
{ $limit: show }
] )
.exec()
这里查询的是?show=2&page=1
。控制台输出为 2 0 1
。那是正确的。
完整错误在这里:
{ [MongoError: exception: the limit must be specified as a number]
name: 'MongoError',
errmsg: 'exception: the limit must be specified as a number',
code: 15957,
ok: 0 }
出于某种原因,show
在 $limit
的情况下被视为字符串,但似乎没有其他问题。我这样做是为了修复它,但我认为这可能是 Express 或 Mongoose/MongoDB 的错误。如果有人知道在哪里提出这个问题,请告诉我。
修复方法是像这样使用 parseInt
:
var show = parseInt( req.query.show ), // the number of items to show per page
您也可以使用一元 + 运算符。 +req.query.show
我正在管道中使用 $limit
执行 Mongoose/MongoDB .aggregate
查询。当我使用数字时,如 2
,它工作正常。如果我设置一个像 testNum = 2
这样的变量,然后执行 {$limit: varNum}
,它工作正常。但是,如果我发送一个 REST 查询并尝试执行 $limit: req.body.show
,它会说该值不是数字。
通过console.log
可以看出是一个数字。管道中的其他查询不会抱怨没有给出数字。这是代码:
var show = req.query.show, // the number of items to show per page
page = req.query.page, // the current page being asked for
stream = req.params.stream, // the type of content to get
skip = ( page > 0 ? (( page - 1 ) * show ) : 0 ), // amount to skip
testNum = 3
console.log( show + " " + skip + " " + page )
Content.aggregate( [
{ $unwind: '$users' },
{ $group: {
_id: '$_id',
title: { $first: '$title' },
description: { $first: '$description' },
images: { $first: '$images' },
url: { $first: '$url' },
saveCount: { $sum: 1 } }
},
{ $sort: { saveCount: -1 } },
{ $skip: skip },
{ $limit: show }
] )
.exec()
这里查询的是?show=2&page=1
。控制台输出为 2 0 1
。那是正确的。
完整错误在这里:
{ [MongoError: exception: the limit must be specified as a number]
name: 'MongoError',
errmsg: 'exception: the limit must be specified as a number',
code: 15957,
ok: 0 }
出于某种原因,show
在 $limit
的情况下被视为字符串,但似乎没有其他问题。我这样做是为了修复它,但我认为这可能是 Express 或 Mongoose/MongoDB 的错误。如果有人知道在哪里提出这个问题,请告诉我。
修复方法是像这样使用 parseInt
:
var show = parseInt( req.query.show ), // the number of items to show per page
您也可以使用一元 + 运算符。 +req.query.show