1

I want to show data from json map to express

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).

and I have this json file :

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
  • One word: Asynchronity Commented Jan 28, 2017 at 10:44

1 Answer 1

1

You can perform a single request to match a list of _id with $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'
            });
        }
    });
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.