Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I've got an issue reading a nested array from JSON(BSON from MongoHQ) using Node and Angular.

JSON snippet: http://pastie.org/9305682. Specifically look for the edges array.

Mongoose model: http://pastie.org/9305685

Basically I call the character from the DB and then attempt to log it to the console with console.log(char); before sending it back to the angular call with res.json(char); 'char' is the returned character from the databased saved as my mongoose model.

Attempting to log the character to the console. I get everything looking normal except for the portions with the nested "effects" arrays. Anywhere they show up I receive the following:

edges:                                                                                                                                                                                                                                        
 [ { name: 'Super Hacker', notes: '', effects: [Object] },                                                                                                                                                                                    
   { name: 'Witty', notes: '', effects: [Object] },                                                                                                                                                                                           
   { name: 'Attractive', notes: '', effects: [Object] },                                                                                                                                                                                      
   { name: 'Encyclopedic Memory',                                                                                                                                                                                                             
     notes: 'Prereq: d8 Smarts',                                                                                                                                                                                                              
     effects: [Object] },                                                                                                                                                                                                                     
   { name: 'Daywalker', notes: '', effects: [Object] },                                                                                                                                                                                       
   { name: 'Tough', notes: '', effects: [Object] } ],    

From here if I try to call it with:

From NodeJS - console.log(char[0].edges[0].effects[0].type); - Returns undefined.

From Angular View - {{cur_char.edges[0].effects[0].type}} - Displays nothing.

Thanks in advance for the help. Let me know if I can provide more in.

share|improve this question
    
Have you tried just console.log(char[0].edges[0].effects[0]), to see if the object is being returned as JSON? –  JoshSGman Jun 19 at 16:50
    
Calling this I get [object Object]. –  Tyrrexx Jun 19 at 18:13

1 Answer 1

up vote 0 down vote accepted

I think what you're asking is how to see more depth to the object in the console output. You can use util.inspect to print out more information:

console.log(util.inspect(char, { depth: 5 }));

By default util.inspect only goes to a depth of 2 which explains why you can only see the contents of the array (1) and the primitive properties of each element in the array (2).

See: http://nodejs.org/api/util.html#util_util_inspect_object_options

share|improve this answer
    
Okay, I've tried this out. I still get the same result of [object] though. –  Tyrrexx Jun 19 at 19:20
    
It appears to work for me. Here is a script I created to test it out. On screen I see effects displayed in console for all the edges. gist.github.com/anonymous/f8f29903010a6f425e03 I named the file char.js and ran it with: node char.js –  Hayes Jun 19 at 19:32
    
Hmm yeah doing it that way works for me too. I believe the issue was that the effects array needed to be treated as its own schema. Regardless I've decided just to keep effects as a single array of strings instead of anbarray of objects. Thanks for the help! –  Tyrrexx Jun 20 at 17:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.