【问题标题】:Select multiple field using select使用 select 选择多个字段
【发布时间】:2021-03-16 12:19:56
【问题描述】:

我要求使用邮递员 这是网址

http://localhost:3000/api/v1/tours?fields=name,price

if(req.query.fields){
        let fields=req.query.fields.split(',').join(" ");
        console.log(fields);
        query=query.select(fields);
    }
    else{
        query=query.select('-__v');
    }

我认为输出只是旅游集合的名称和价格字段,但我没有得到任何结果

请帮我解决这个问题

但是当我同样的 sort 方法工作完美 http://localhost:3000/api/v1/tours?sort=price,averageRating

if(req.query.sort){
    let sortBy=req.query.sort.split(',').join(" ");
    query=query.sort(sortBy);
}

else{

    query=query.sort('-createdAt');
}

【问题讨论】:

  • 您的代码看起来不错。你得到什么结果?你在期待什么?两种场景的代码还有什么不同吗?

标签: node.js mongoose select filter limit


【解决方案1】:

我遇到了同样的问题。在代码中,您声明了排除的字段,我在数组中有错字:

const excludeFields = ['page', 'sort', 'limit', 'fields'];

我拼错了fields(原来是fileds

【讨论】:

    猜你喜欢
    • 2017-11-30
    • 2017-04-06
    • 2021-10-15
    • 2019-07-03
    • 1970-01-01
    • 2012-10-13
    • 1970-01-01
    • 2011-02-13
    • 1970-01-01
    相关资源
    最近更新 更多