如何限制显示的对象数?

时间:2019-06-18 13:14:24

标签: node.js express mongoose

如何限制通过猫鼬显示的结果?我只想显示Carauction模式中包含的结果数组的前5个对象:

router.get('/app/:cars_getroute', ensureAuthenticated, (req, res, next) => {
Promise.all([Car.findOne({ cars_getroute: req.params.cars_getroute }), Carauction.findOne({ cars_getroute: req.params.cars_getroute }), Carauctionstats.findOne({cars_getroute: req.params.cars_getroute})])
    .then(result => {
        const [cars, carauctions, carauctionstats] = result;

        if (User.isCharged = true) {
            res.render('app/fichecar', {
                results: carauctions.results,
        })...

我的模式:

const CarauctionSchema = new Schema({
    objectID: {
        type: Number
    },
    cars_getroute: {
        type: String
    },
    gm_url: {
        type: String
    },
    "results": {
        type: [{
            marque: {
                type: String
            },
            model: {
                type: String
            }...}]
    }

我想限制为10个结果对象。我尝试使用Carauction.find(carauctions.results).limit(10),但它不起作用。

然后我用车把渲染结果项:

{{#each results as |auction|}}

                    <a target="_blank" rel="noopener noreferrer" href="{{auction.auction_url}}" class="results__box--hover">
                        <div class="results__container--box">
                            <div class="results__container--img">
                                <img src="{{auction.image_urls}}">
                            </div>
                            <div class="results__container--left">
                                <div class="results__container--model">
                                    <span>{{auction.model_year}} {{auction.marque}} {{auction.model}}</span>
                                </div>
                                <div class="results__container--auction">
                                    <span class="results__auction_house">{{auction.auction_house}}</span>
                                    <span>{{auction.auction_country}}</span>
                                    <span class="results__auction_date">{{auction.auction_date}}</span>


            {{/each}}

1 个答案:

答案 0 :(得分:0)

您可以使用carauctions.results.slice(0,10)限制结果。这样最多可以输出10个项目,但是如果少于10个项目则不会造成问题。