-1

I wanto to get a length of an object using the mongoose lib

here is my code

const mongo = require("../../server") // model call
    
     {...}
    
    var query = 'email@email.com'; //get an email
        var db = mongo.connection;
        
        db.db.collection("user").findOne({email:query}, function(err, result) {
    
          if (err) throw err; 
          if ( result.length > 0) {...} // here I want to get my object length

In result.length Im getting an error because there's no length in my object , so someone know's how can I get it ?

1 Answers1

0

You can use Object.keys(result).length to get the length of entries

Shahzaib
  • 313
  • 2
  • 8
  • This one was returning me a keys is not a function , but I used a Object.values(MYOBJECT).length and it worked , but thanks for coming by –  Oct 27 '21 at 11:47
  • Glad to hear that it worked but its strange why Object.keys didn't work as Object prototype has both of these methods – Shahzaib Oct 28 '21 at 16:14