gpt4 book ai didi

node.js - 使用 req.query.property 时出现“限制必须指定为数字”错误

转载 作者:可可西里 更新时间:2023-11-01 09:11:25 25 4
gpt4 key购买 nike

我正在管道中使用 $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

关于node.js - 使用 req.query.property 时出现“限制必须指定为数字”错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29424959/

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