无法在Mongoose和Express中调用嵌套键(未定义)

时间:2019-06-08 12:59:24

标签: node.js mongodb express mongoose

我有一个显示结果的猫鼬模式。我可以显示结果数组,但不能显示其中的内容。

{
    "_id": {
        "$oid": "5cfa15e123d2f414da760635"
    },
    "objectID": 21078,
    "cars_getroute": "volvo-123-gt-coach-1967-1968",
    "gm_url": "https://www.url.com",
    "results": [
        {
            "marque": "Volvo",
            "model": "Amazon 123 GT",
            "model_year": "1967",
            "price_str": "£4 982",
            "price_int": 4982,
            "price_currency": "£",
            "sold": true,
            "auction_house": "Anglia Car Auctions",
            "auction_country": "Grande-Bretagne",
            "auction_date": "25 août 2018",
            "auction_datetime": "2018-08-25",
            "auction_url": null,
            "image_urls": null,
            "price_int_eu": 5523
        },
        {
            "marque": "Volvo",
            "model": "Amazon 123 GT",
            "model_year": "1968",
            "price_str": "CHF9 000",
            "price_int": 9000,
            "price_currency": "CHF",
            "sold": true,
            "auction_house": "Oldtimer Galerie",
            "auction_country": "Suisse",
            "auction_date": "29 avril 2017",
            "auction_datetime": "2017-04-29",
            "auction_url": null,
            "image_urls": "https://www.url.com/img/auctions/car/thumb/17-4-11-f8a85a227775570cdeb80ba0a437cc40.jpg",
            "price_int_eu": 8309
        },
        {
            "marque": "Volvo",
            "model": "Amazon 123 GT",
            "model_year": "1968",
            "price_str": "Estimate £6 000 - £7 000 (unsold)",
            "price_int": null,
            "price_currency": null,
            "sold": false,
            "auction_house": "Herefordshire Vintage Auctions",
            "auction_country": "Grande-Bretagne",
            "auction_date": "20 octobre 2016",
            "auction_datetime": "2016-10-20",
            "auction_url": "http://www.url.com/Motor_details.php?pid=188&motor=188_01.jpg",
            "image_urls": "https://www.url.com/img/auctions/car/2016-10-4a5a09eeb5c685e053e421c45656dcf7.jpg",
            "price_int_eu": null
        }
    ]
}

模型:

    const DemocarauctionSchema = new Schema({
    objectID: {
        type: Number
    },
    cars_getroute: {
        type: String
    },
    gm_url: {
        type: String
    },
    "results": { type: [{
        marque: {
            type: String
        },
        model: {
            type: String
        },
        model_year: {
            type: String
        },
        price_str: {
            type: String
        },
        prince_int: {
            type: Number
        },
        price_currency: {
            type: String
        },
        sold: {
            type: Boolean
        },
        auction_house: {
            type: String
        },
        auction_country: {
            type: String
        },
        auction_date: {
            type: String
        },
        auction_datetime: {
            type: String
        },
        auction_url: {
            type: String
        },
        image_urls: {
            type: String
        },
        price_int_eu: {
            type: Number
        },
    }]}

},
    {
        collection: 'democarficheauction'
    });

当我打电话给console.log(democarauctions.results.marque)时,什么也没出现。 我的架构有问题吗?因为“结果”数组中有多个“ marque”键? 与gm_url,objectId和cars_getroute配合使用效果很好。

编辑:这是我的快速路线:

router.get('/demo/:cars_getroute', (req, res, next) => {
    Promise.all([Democar.findOne({ cars_getroute: req.params.cars_getroute }), Democarauction.findOne({ cars_getroute: req.params.cars_getroute })])
        .then(result => {
            const [democars, democarauctions] = result;
            console.log(democarauctions.results.marque)
            res.render('demo/fichecar-demo', {
                marque: democars.marque,
                modele: democars.modele,
                sous_modele: democars.sous_modele,
                sous_modele2: democars.sous_modele2,
                type: democars.type,
                precision: democars.precision,
                years_interval: democars.years_interval,
                cote_actual: democars.cote_actual.toLocaleString('fr-FR'),
                img_url: democars.img_url,
                cote_1989_eu_excp: democars.cote.cote_1989.cote_1989_eu.cote_1989_excp,
                cote_1989_eu_concours: democars.cote.cote_1989.cote_1989_eu.cote_1989_concours,
                cote_1989_eu_base: democars.cote.cote_1989.cote_1989_eu.cote_1989_base,
                cote_1989_eu_be: democars.cote.cote_1989.cote_1989_eu.cote_1989_be,
                cote_1989_eu_me: democars.cote.cote_1989.cote_1989_eu.cote_1989_me,
                cote_1989_eu_ar: democars.cote.cote_1989.cote_1989_eu.cote_1989_ar,
                cote_1989_eu_epa: democars.cote.cote_1989.cote_1989_eu.cote_1989_epa,
                cote_2004_excp: democars.cote.cote_2004.cote_2004_excp,
                cote_2004_concours: democars.cote.cote_2004.cote_2004_concours,
                cote_2004_base: democars.cote.cote_2004.cote_2004_base,
                cote_2004_be: democars.cote.cote_2004.cote_2004_be,
                cote_2004_me: democars.cote.cote_2004.cote_2004_me,
                cote_2004_ar: democars.cote.cote_2004.cote_2004_ar,
                cote_2004_epa: democars.cote.cote_2004.cote_2004_epa,
                cote_2014_excp: democars.cote.cote_2014.cote_2014_excp,
                cote_2014_concours: democars.cote.cote_2014.cote_2014_concours,
                cote_2014_base: democars.cote.cote_2014.cote_2014_base,
                cote_2014_be: democars.cote.cote_2014.cote_2014_be,
                cote_2014_me: democars.cote.cote_2014.cote_2014_me,
                cote_2014_ar: democars.cote.cote_2014.cote_2014_ar,
                cote_2014_epa: democars.cote.cote_2014.cote_2014_epa,
                cote_2017_excp: democars.cote.cote_2017.cote_2017_excp,
                cote_2017_concours: democars.cote.cote_2017.cote_2017_concours,
                cote_2017_base: democars.cote.cote_2017.cote_2017_base,
                cote_2017_be: democars.cote.cote_2017.cote_2017_be,
                cote_2017_me: democars.cote.cote_2017.cote_2017_me,
                cote_2017_ar: democars.cote.cote_2017.cote_2017_ar,
                cote_2017_epa: democars.cote.cote_2017.cote_2017_epa,
                cote_2019_excp: democars.cote.cote_2019.cote_2019_excp,
                cote_2019_concours: democars.cote.cote_2019.cote_2019_concours,
                cote_2019_base: democars.cote.cote_2019.cote_2019_base,
                cote_2019_be: democars.cote.cote_2019.cote_2019_be,
                cote_2019_me: democars.cote.cote_2019.cote_2019_me,
                cote_2019_ar: democars.cote.cote_2019.cote_2019_ar,
                cote_2019_epa: democars.cote.cote_2019.cote_2019_epa,
                cote_actual_chart: democars.cote_actual,
                cote_actual_excp: democars.cote.cote_2019.cote_2019_excp.toLocaleString('fr-FR'),
                cote_actual_concours: democars.cote.cote_2019.cote_2019_concours.toLocaleString('fr-FR'),
                cote_actual_base: democars.cote.cote_2019.cote_2019_base.toLocaleString('fr-FR'),
                cote_actual_be: democars.cote.cote_2019.cote_2019_be.toLocaleString('fr-FR'),
                cote_actual_me: democars.cote.cote_2019.cote_2019_me.toLocaleString('fr-FR'),
                cote_actual_ar: democars.cote.cote_2019.cote_2019_ar.toLocaleString('fr-FR'),
                cote_actual_epa: democars.cote.cote_2019.cote_2019_epa.toLocaleString('fr-FR'),
                perfo_1989: democars.performance.perfo1989,
                perfo_2004: democars.performance.perfo2004,
                perfo_2014: democars.performance.perfo2014,
                perfo_2017: democars.performance.perfo2017,

                results: democarauctions.results,
marqueauction: democarauctions.results.marque

            }


            });
        })
        .catch(err => {
            // handle error. 
            console.log(err);
        })

});

这是我的HTML模板:

<div class="results__container--content">
                {{#each results}}
                <div class="results__container--box">
                    <h1>{{marqueauction}}</h1>
                </div>
                {{else}}
                <div class="results__container--box">
                    <p>Aucun résultat d'enchères n'est disponible pour ce modèle.</p>
                </div>
                {{/each}}
            </div>

1 个答案:

答案 0 :(得分:0)

results是一个数组,您应该在数组上循环以获取marque的值

let marqueauction = [];
for(let i =0; i < democarauctions.results.length; i++) {
    marqueauction.push(democarauctions.results[i].marque)
}

res.render('demo/fichecar-demo', {
  marqueauction: marqueauction
});

您现在可以通过遍历marqueauction来在HTML中呈现字幕。