Problems reading MongoDb collection with Node.js

30 views
Skip to first unread message

Luis Alberto Grijalva Romero

unread,
Feb 10, 2015, 2:33:37 AM2/10/15
to mongoo...@googlegroups.com


I have problems reading this collection with mongoose and Node.js:

I have one JSON inside my Collection and i try to read this with this code:

materias.find().exec(function(err1,materias){
        if(err1){console.log(err1); return next();}
        for(x=0;x<materias.length;x++){
            //console.log(materias[x].Nombre);
            //var arreglo=materias[x].Horario[0].Dia; // TRY ONE
            var arreglo=JSON.parse(materias[x].Horario[0]); // TRY TWO
            console.log(arreglo[0]);

        }
         //RESPONSE TRY ONE
         console.log(arreglo) UNDEFINED
         //RESPONSE TRY TWO
         undefined [ 
         //if I use JSON.stringify the response is {[object][object]...
Suggestions...

Anthony Ettinger

unread,
Feb 10, 2015, 2:59:20 AM2/10/15
to mongoo...@googlegroups.com
materias is not an array, given your example.

--
Documentation - http://mongoosejs.com/
Plugins - http://plugins.mongoosejs.com/
Bug Reports - http://github.com/learnboost/mongoose
Production Examples - http://mongoosejs.tumblr.com/
StackOverflow - http://stackoverflow.com/questions/tagged/mongoose
Google Groups - https://groups.google.com/forum/?fromgroups#!forum/mongoose-orm
Twitter - https://twitter.com/mongoosejs
IRC - #mongoosejs
---
You received this message because you are subscribed to the Google Groups "Mongoose Node.JS ODM" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongoose-orm...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

Luis Alberto Grijalva Romero

unread,
Feb 10, 2015, 5:28:49 PM2/10/15
to mongoo...@googlegroups.com
is not an array but if i try with this:
var arreglo = materias[x].Horario[0]; console.log(arreglo); //Response: [[
console.log(materias[x].Nombre)// Response: Tópicos Selectos de Programación
console
.log(materias[x].Horario) // Response: [object Object],[object Object],[object Object],[object Object],
console
.log(materias[x].Horario[0].Dia); // Response: Undefined
console
.log(materias[x].Horario.Dia[0]) // Response: Cannot read property '0' of undefined


Reply all
Reply to author
Forward
0 new messages