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.