2

It's to my understanding that there is no .toObect() in JavaScript but it is used in mongoose to change the mongoose documents to an object so you can use JavaScript built in functions.

I don't have a grasp on when to use it. sometimes when i get an array of docs I could use forEach on the returned array and then other times I would spend 20 minutes working out why the forEach doesn't work. then I would add .toObject to the returned Array and the forEach would work. something like that. I'm not sure about my memory if it was forEach or something else.

Anyways this is the latest weird issue. I'm in EJS and I needed to do <% console.log("typeof", user.toObject().hasOwnProperty("facebook")) %> to work instead of <% console.log("typeof", user.hasOwnProperty("facebook")) %> . The one with the .toObject() consoles : typeof true the one without consoles typeof false . That seems weird to me. Is user a mongoose document? How could I use .toObject in ejs? Oh wait a minute. Im just thinking is it because it is in the "<%%>" it gets connected to the server side code and maybe I have mongoose required in my server.js Why did I have to use toObject to get the true value?

any ways I didn't think I needed to use .toObject()

This is what I have in my .js file : res.render("editProfile", {aboutUser : returnedUser, user : req.user}); I think req.user is from passport not mongoose.

user obj

{ _id: 581a2chan3changed727, defaultImage: 'https://scontefrfrxxffbcdn.net/v/t1.0-1/s148x148/29731_575634691740_1085883_n.jpg?oh=797bc81addf3611309changedoe=588AC839', urlname: 'Jacchanged', momented: 'Nov 2, 2016', username: 'Jack Schuldenfrei', __v: 0, usefulness: [], createdOn: Wed Nov 02 2016 14:15:47 GMT-0400 (Eastern Daylight Time), shortId: 'rkn8powgl', pointsRecord: [], repPoints: 0, facebook: { posts: '{"email":"jschuldenfreiGTGTGTgchanged,"name":"Jack Schuchange","gender":"male","picture":{"data":{"height":148,"is_silhouette":false,"url":"https:\\/\\frfr.net\\/v\\/t1.0-1\\/s148x148\\/29731_575634691740_1fr5883_n.jpg?oh=797bc81addf36113e0933a67eef32ab9&oe=588AC839","width":93}},"id":"10100584555845400"}', gender: 'male', email: 'jschuldenfredu', name: 'Jack changes', token: 'EAAPs693O1UMBAHwTcyZAPtRiXrwvxrBWhGCJZCQlAIfHRZAiCLY3tYDVoviB4yDrK68WrsUnuxlcHfUJE984aAvWOnFZASqbUjYZAhHnsL0mFCZCNRQwsn3oJn1acu1qnSPFko6I3ShZAtPIMumrVlpVxR0ZD', id: '1010058386584400' }, reviews: [] } { _id: 581a2d53380e4c70ac728, userId: 581a2d380e4c7037aac727, username: 'Jack changed', urlname: 'Jachanged', __v: 0, question1: { name: 'This is for question1' } }

I get that user object by doing <%=user%> in the ejs file.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
jack blank
  • 5,073
  • 7
  • 41
  • 73
  • 1
    Mongoose represents MongoDB schemas. The only properties that Mongoose objects implement are ones provided by the mongoose APIs. They don't represent actual JavaScript objects. To get an actual JavaScript object you have to call (oddly enough) `toObject()`. And I think maybe we need less details – Tibrogargan Nov 02 '16 at 23:24
  • Mongoose does return an object, but it's most likely a cursor, not the data object you want. If you add an actual question, and show us how you get that object, it would be easier. – adeneo Nov 02 '16 at 23:27
  • `req.user` is supplied by passport. I believe that that is normal and I pass it to `ejs`. so I'm surprised I need to use `toObject`. This guy mentions an issue about `toObject`. I'm wondering if that might be causing me issues. http://stackoverflow.com/a/39019417/1893672 – jack blank Nov 02 '16 at 23:31
  • It's so weird because I was able to get access to the data by doing something like `user.facebook` but I couldn't use the hasOwnProperty without the `toObject()` and if just did the condition test with `user.facebook` it would give me `false`, the wrong response. – jack blank Nov 02 '16 at 23:37

1 Answers1

1

What you want is .lean()

Usually passport has a serialization configuration that looks like

// Serialize sessions
passport.serializeUser(function (user, done) {
    done(null, user.id);
});

// Deserialize sessions
passport.deserializeUser(function (id, done) {
    User.findOne({
        _id: id
    }, '-salt -password', function (err, user) {
        done(err, user);
    });
});

Change the deserialization process like

// Deserialize sessions
passport.deserializeUser(function (id, done) {
    User.findOne({
        _id: id
    }, '-salt -password')
    .lean() // lean converts mongoose.Document to Plain Javascript Object
    .exec(function (err, user) {
        console.log(user instanceof mongoose.Document) // false
        done(err, user); 
    });
});
Medet Tleukabiluly
  • 11,662
  • 3
  • 34
  • 69