Node.js從mongodb渲染json地圖

[英]Node.js render json map from mongodb


I want to show data from json map to express

我想顯示來自json map的數據來表達

this my code

這是我的代碼

var b = det.map(function(h) {
    var soalid = h.id_soal;
    var idarray = [ ];
    for (var i = 0; i < soalid.length; i++) {

        Soal.findOne({
            _id: soalid[i]
        }, function (err, sol) {

            idarray.push(sol);
            console.log("ping:" + idarray);
        });

    }
    res.render('ujian/viewDetail',{ujian: iduji, detail: det, soal: idarray, title: 'Lihat form ujian'});
});

I have try run to console I get data from json but not showing on express (web).

我嘗試運行到控制台我從json獲取數據但沒有在express(web)上顯示。

and I have this json file :

我有這個json文件:

ping:{ 
     _id: 58516fc32aeffd103cdda179,
     jurusan: 'IPA',
     matapelajaran: 'Fisika',
     soal: 'agae',
     jawabana: 'hjg',
     jawabanb: 'h',
     jawabanc: 'hbh',
     jawaband: 'hbh',
     jawabane: 'h',
     kuncijawaban: 'hb',
     __v: 0 
},
{ 
    _id: 585a95167467c5185e2f2ee9,
    jurusan: 'IPA',
    matapelajaran: 'Fisika',
    soal: 'agega',
    jawabana: 'hjkbkjb',
    jawabanb: 'kjbkjb',
    jawabanc: 'kjbkjb',
    jawaband: 'kjbkjb',
    jawabane: 'kjbkbkj',
    kuncijawaban: 'kjbk',
    __v: 0 
}

1 个解决方案

#1


1  

You can perform a single request to match a list of _id with $in :

您可以執行單個請求以將_id列表與$ in匹配:

var b = det.map(function(h) {

    Soal.find("_id": {
        $in: h.id_soal
    }, function(err, solArray) {
        if (err) {
            console.log(err);
        } else {
            res.render('ujian/viewDetail', {
                ujian: iduji,
                detail: det,
                soal: solArray,
                title: 'Lihat form ujian'
            });
        }
    });
});

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2017/01/28/72511905fd7ca8eee074586455e1ec4f.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com