0

I have code that runs mongoose and successfully returns the following results.

Why can't I do the following and get values from my object?

Object.keys(result).forEach(function(record){
            console.log("The value of result[record]: ", result[record]);
        })

How do I iterate through this object to get the different keys to get the values in both lists and objects?

I'm using plain vanilla javascript. But I wouldn't mind some of the newer ES options.

{ owner: { fullName: 'xxx', userName: 'xxx' },
  members: [ { userName: 'xxx', fullName: 'xxx', position: '' } ],
  admins: [ 'xxx','ashg' ],
  _id: 5a482302a469a068edc004e3,
  type: 'xxxx',
  name: 'xxxx xxxx',
  descrip: 'xxxx',
  startDate: '2018-01-01',
  endDate: ''
}

Here is a simpler example of what I want to replicate and that works exactly as expected:

var o={a:1,b:2,c:3};
var val;

Object.keys(o).forEach(function(key) {
    val = o[key];
    console.log(val);
});

Output: 1,2,3
mo_maat
  • 2,110
  • 12
  • 44
  • 72

2 Answers2

1

Assuming result is a Mongoose document, you can call result.toObject() to convert it into a plain JS object so that you can effectively use methods like Object.keys on it.

var o = result.toObject();
Object.keys(o).forEach(function(key) {
    console.log(o[key]);
});

But you can also use the eachPath method of the document's schema to get the document properties and iterate that way as well:

result.schema.eachPath((path, schemaType) => {
    console.log(result[path]);
}); 
JohnnyHK
  • 305,182
  • 66
  • 621
  • 471
  • Thanks! That did it! Just so I understand, what is the type of the mongoose returned object? `console.log(typeof(result));` shows `object`. Also, is this the best/quickest most efficient way to handle accessing nested objects or values in mongoose returned data or any object in general? My ultimate goal was to access and manipulate the nested members object list in my returned mongoose object. With your suggestion I'm able to convert to an object and use the usual javascript object and array methods, but it seems a bit slow when dealing with the nested objects. – mo_maat Jan 02 '18 at 16:10
  • 1
    It's an instance of your model. If you don't need the model instance, call [`lean()`](http://mongoosejs.com/docs/api.html#query_Query-lean) on your query chain to directly get plain JS objects and better performance. See [here](https://stackoverflow.com/a/18070111/1259510). – JohnnyHK Jan 03 '18 at 01:51
-1

Why can't I do the following and get values from my object?

Because in your code, you are trying to access value by doing result[record], while record is not the key to the value, it is the value only.

Simply printing the record will do it

    result.forEach(function(record){
        console.log("The value of record: ", record );
        console.log("name is: ", record.name );
    });
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
  • I'm not sure about this answer. The value of `console.log("result[record]: ", record );` outputs: `The value of object[key]: $__ The value of object[key]: isNew The value of object[key]: errors The value of object[key]: _doc The value of object[key]: $init` – mo_maat Jan 02 '18 at 14:27
  • And the suggest answer of using `result.forEach` does not work because I get `TypeError: result.forEach is not a function` – mo_maat Jan 02 '18 at 14:28
  • Please update your question to share what output you are getting with `console.log(result)` – gurvinder372 Jan 02 '18 at 14:28
  • Is `result` not the output of mongoose query? – gurvinder372 Jan 02 '18 at 14:28
  • the `result.forEach` syntax works when my mongoose call is returning and object array. In that case I can do `record.` and get my values. I don't understand why this seems to be so complicated. Following the instructions on MDN for looping through objects to get the keys and then retrieve values based on those keys does not seem to work for me. I'm missing something obviously... `https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys` – mo_maat Jan 02 '18 at 14:31
  • How did you got this `result`? – gurvinder372 Jan 02 '18 at 14:38
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/162353/discussion-between-mo-maat-and-gurvinder372). – mo_maat Jan 02 '18 at 14:38