Model.findOne not returning docs but returning a wrapper object











up vote
4
down vote

favorite
4












I have defined a Model with mongoose like this:



var mongoose = require("mongoose")
var Schema = mongoose.Schema

var userObject = Object.create({
alias: String,
email: String,
password: String,
updated: {
type: Date,
default: Date.now
}
})

var userSchema = new Schema(userObject, {strict: false})
var User = mongoose.model('User', userSchema)

module.exports = User


Then I created a user that I can perfectly find through mongo console like this:



db.users.findOne({ email: "coco@coco.com" });
{
"_id" : ObjectId("55e97420d82ebdea3497afc7"),
"password" : "caff3a46ebe640e5b4175a26f11105bf7e18be76",
"gravatar" : "a4bfba4352aeadf620acb1468337fa49",
"email" : "coco@coco.com",
"alias" : "coco",
"updated" : ISODate("2015-09-04T10:36:16.059Z"),
"apps" : [ ],
"__v" : 0
}


However, when I try to access this object through a node.js with mongoose, the object a retrieve is not such doc, but a wrapper:



This piece of code...



// Find the user for which the login queries
var User = require('../models/User')
User.findOne({ email: mail }, function(err, doc) {
if (err) throw err
if (doc) {
console.dir(doc)
if(doc.password == pass) // Passwords won't match


Produces this output from console.dir(doc)...



{ '$__': 
{ strictMode: false,
selected: undefined,
shardval: undefined,
saveError: undefined,
validationError: undefined,
adhocPaths: undefined,
removing: undefined,
inserting: undefined,
version: undefined,
getters: {},
_id: undefined,
populate: undefined,
populated: undefined,
wasPopulated: false,
scope: undefined,
activePaths: { paths: [Object], states: [Object], stateNames: [Object] },
ownerDocument: undefined,
fullPath: undefined,
emitter: { domain: null, _events: {}, _maxListeners: 0 } },
isNew: false,
errors: undefined,
_doc:
{ __v: 0,
apps: ,
updated: Fri Sep 04 2015 12:36:16 GMT+0200 (CEST),
alias: 'coco',
email: 'coco@coco.com',
gravatar: 'a4bfba4352aeadf620acb1468337fa49',
password: 'caff3a46ebe640e5b4175a26f11105bf7e18be76',
_id: { _bsontype: 'ObjectID', id: 'Uét Ø.½ê4¯Ç' } },
'$__original_validate': { [Function] numAsyncPres: 0 },
validate: [Function: wrappedPointCut],
_pres: { '$__original_validate': [ [Object] ] },
_posts: { '$__original_validate': } }


Therefore, passwords won't match because doc.password is undefined.



Why is this caused?










share|improve this question


















  • 1




    Use console.log, not console.dir to sanely log Mongoose model instances.
    – JohnnyHK
    Sep 4 '15 at 13:04










  • Yeah, as a fact console.log(doc) only printed the wrapped document, but the password validation still did not work and doc.password still returned undefined without console.dir(doc).
    – jsdario
    Sep 4 '15 at 13:49















up vote
4
down vote

favorite
4












I have defined a Model with mongoose like this:



var mongoose = require("mongoose")
var Schema = mongoose.Schema

var userObject = Object.create({
alias: String,
email: String,
password: String,
updated: {
type: Date,
default: Date.now
}
})

var userSchema = new Schema(userObject, {strict: false})
var User = mongoose.model('User', userSchema)

module.exports = User


Then I created a user that I can perfectly find through mongo console like this:



db.users.findOne({ email: "coco@coco.com" });
{
"_id" : ObjectId("55e97420d82ebdea3497afc7"),
"password" : "caff3a46ebe640e5b4175a26f11105bf7e18be76",
"gravatar" : "a4bfba4352aeadf620acb1468337fa49",
"email" : "coco@coco.com",
"alias" : "coco",
"updated" : ISODate("2015-09-04T10:36:16.059Z"),
"apps" : [ ],
"__v" : 0
}


However, when I try to access this object through a node.js with mongoose, the object a retrieve is not such doc, but a wrapper:



This piece of code...



// Find the user for which the login queries
var User = require('../models/User')
User.findOne({ email: mail }, function(err, doc) {
if (err) throw err
if (doc) {
console.dir(doc)
if(doc.password == pass) // Passwords won't match


Produces this output from console.dir(doc)...



{ '$__': 
{ strictMode: false,
selected: undefined,
shardval: undefined,
saveError: undefined,
validationError: undefined,
adhocPaths: undefined,
removing: undefined,
inserting: undefined,
version: undefined,
getters: {},
_id: undefined,
populate: undefined,
populated: undefined,
wasPopulated: false,
scope: undefined,
activePaths: { paths: [Object], states: [Object], stateNames: [Object] },
ownerDocument: undefined,
fullPath: undefined,
emitter: { domain: null, _events: {}, _maxListeners: 0 } },
isNew: false,
errors: undefined,
_doc:
{ __v: 0,
apps: ,
updated: Fri Sep 04 2015 12:36:16 GMT+0200 (CEST),
alias: 'coco',
email: 'coco@coco.com',
gravatar: 'a4bfba4352aeadf620acb1468337fa49',
password: 'caff3a46ebe640e5b4175a26f11105bf7e18be76',
_id: { _bsontype: 'ObjectID', id: 'Uét Ø.½ê4¯Ç' } },
'$__original_validate': { [Function] numAsyncPres: 0 },
validate: [Function: wrappedPointCut],
_pres: { '$__original_validate': [ [Object] ] },
_posts: { '$__original_validate': } }


Therefore, passwords won't match because doc.password is undefined.



Why is this caused?










share|improve this question


















  • 1




    Use console.log, not console.dir to sanely log Mongoose model instances.
    – JohnnyHK
    Sep 4 '15 at 13:04










  • Yeah, as a fact console.log(doc) only printed the wrapped document, but the password validation still did not work and doc.password still returned undefined without console.dir(doc).
    – jsdario
    Sep 4 '15 at 13:49













up vote
4
down vote

favorite
4









up vote
4
down vote

favorite
4






4





I have defined a Model with mongoose like this:



var mongoose = require("mongoose")
var Schema = mongoose.Schema

var userObject = Object.create({
alias: String,
email: String,
password: String,
updated: {
type: Date,
default: Date.now
}
})

var userSchema = new Schema(userObject, {strict: false})
var User = mongoose.model('User', userSchema)

module.exports = User


Then I created a user that I can perfectly find through mongo console like this:



db.users.findOne({ email: "coco@coco.com" });
{
"_id" : ObjectId("55e97420d82ebdea3497afc7"),
"password" : "caff3a46ebe640e5b4175a26f11105bf7e18be76",
"gravatar" : "a4bfba4352aeadf620acb1468337fa49",
"email" : "coco@coco.com",
"alias" : "coco",
"updated" : ISODate("2015-09-04T10:36:16.059Z"),
"apps" : [ ],
"__v" : 0
}


However, when I try to access this object through a node.js with mongoose, the object a retrieve is not such doc, but a wrapper:



This piece of code...



// Find the user for which the login queries
var User = require('../models/User')
User.findOne({ email: mail }, function(err, doc) {
if (err) throw err
if (doc) {
console.dir(doc)
if(doc.password == pass) // Passwords won't match


Produces this output from console.dir(doc)...



{ '$__': 
{ strictMode: false,
selected: undefined,
shardval: undefined,
saveError: undefined,
validationError: undefined,
adhocPaths: undefined,
removing: undefined,
inserting: undefined,
version: undefined,
getters: {},
_id: undefined,
populate: undefined,
populated: undefined,
wasPopulated: false,
scope: undefined,
activePaths: { paths: [Object], states: [Object], stateNames: [Object] },
ownerDocument: undefined,
fullPath: undefined,
emitter: { domain: null, _events: {}, _maxListeners: 0 } },
isNew: false,
errors: undefined,
_doc:
{ __v: 0,
apps: ,
updated: Fri Sep 04 2015 12:36:16 GMT+0200 (CEST),
alias: 'coco',
email: 'coco@coco.com',
gravatar: 'a4bfba4352aeadf620acb1468337fa49',
password: 'caff3a46ebe640e5b4175a26f11105bf7e18be76',
_id: { _bsontype: 'ObjectID', id: 'Uét Ø.½ê4¯Ç' } },
'$__original_validate': { [Function] numAsyncPres: 0 },
validate: [Function: wrappedPointCut],
_pres: { '$__original_validate': [ [Object] ] },
_posts: { '$__original_validate': } }


Therefore, passwords won't match because doc.password is undefined.



Why is this caused?










share|improve this question













I have defined a Model with mongoose like this:



var mongoose = require("mongoose")
var Schema = mongoose.Schema

var userObject = Object.create({
alias: String,
email: String,
password: String,
updated: {
type: Date,
default: Date.now
}
})

var userSchema = new Schema(userObject, {strict: false})
var User = mongoose.model('User', userSchema)

module.exports = User


Then I created a user that I can perfectly find through mongo console like this:



db.users.findOne({ email: "coco@coco.com" });
{
"_id" : ObjectId("55e97420d82ebdea3497afc7"),
"password" : "caff3a46ebe640e5b4175a26f11105bf7e18be76",
"gravatar" : "a4bfba4352aeadf620acb1468337fa49",
"email" : "coco@coco.com",
"alias" : "coco",
"updated" : ISODate("2015-09-04T10:36:16.059Z"),
"apps" : [ ],
"__v" : 0
}


However, when I try to access this object through a node.js with mongoose, the object a retrieve is not such doc, but a wrapper:



This piece of code...



// Find the user for which the login queries
var User = require('../models/User')
User.findOne({ email: mail }, function(err, doc) {
if (err) throw err
if (doc) {
console.dir(doc)
if(doc.password == pass) // Passwords won't match


Produces this output from console.dir(doc)...



{ '$__': 
{ strictMode: false,
selected: undefined,
shardval: undefined,
saveError: undefined,
validationError: undefined,
adhocPaths: undefined,
removing: undefined,
inserting: undefined,
version: undefined,
getters: {},
_id: undefined,
populate: undefined,
populated: undefined,
wasPopulated: false,
scope: undefined,
activePaths: { paths: [Object], states: [Object], stateNames: [Object] },
ownerDocument: undefined,
fullPath: undefined,
emitter: { domain: null, _events: {}, _maxListeners: 0 } },
isNew: false,
errors: undefined,
_doc:
{ __v: 0,
apps: ,
updated: Fri Sep 04 2015 12:36:16 GMT+0200 (CEST),
alias: 'coco',
email: 'coco@coco.com',
gravatar: 'a4bfba4352aeadf620acb1468337fa49',
password: 'caff3a46ebe640e5b4175a26f11105bf7e18be76',
_id: { _bsontype: 'ObjectID', id: 'Uét Ø.½ê4¯Ç' } },
'$__original_validate': { [Function] numAsyncPres: 0 },
validate: [Function: wrappedPointCut],
_pres: { '$__original_validate': [ [Object] ] },
_posts: { '$__original_validate': } }


Therefore, passwords won't match because doc.password is undefined.



Why is this caused?







node.js mongodb mongoose nodes






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Sep 4 '15 at 11:53









jsdario

1,67912155




1,67912155








  • 1




    Use console.log, not console.dir to sanely log Mongoose model instances.
    – JohnnyHK
    Sep 4 '15 at 13:04










  • Yeah, as a fact console.log(doc) only printed the wrapped document, but the password validation still did not work and doc.password still returned undefined without console.dir(doc).
    – jsdario
    Sep 4 '15 at 13:49














  • 1




    Use console.log, not console.dir to sanely log Mongoose model instances.
    – JohnnyHK
    Sep 4 '15 at 13:04










  • Yeah, as a fact console.log(doc) only printed the wrapped document, but the password validation still did not work and doc.password still returned undefined without console.dir(doc).
    – jsdario
    Sep 4 '15 at 13:49








1




1




Use console.log, not console.dir to sanely log Mongoose model instances.
– JohnnyHK
Sep 4 '15 at 13:04




Use console.log, not console.dir to sanely log Mongoose model instances.
– JohnnyHK
Sep 4 '15 at 13:04












Yeah, as a fact console.log(doc) only printed the wrapped document, but the password validation still did not work and doc.password still returned undefined without console.dir(doc).
– jsdario
Sep 4 '15 at 13:49




Yeah, as a fact console.log(doc) only printed the wrapped document, but the password validation still did not work and doc.password still returned undefined without console.dir(doc).
– jsdario
Sep 4 '15 at 13:49












1 Answer
1






active

oldest

votes

















up vote
5
down vote



accepted










That's exactly the purpose of mongoose, wrapping mongo objects. It's what provides the ability to call mongoose methods on your documents. If you'd like the simple object, you can call .toObject() or use a lean query if you don't plan on using any mongoose magic on it at all. That being said, the equality check should still hold as doc.password returns doc._doc.password.






share|improve this answer

















  • 1




    Great. It worked! Thank you really. Do you know why in several tutorials and snippets –like this one in mongoose docs– the method .toObject() is not used?
    – jsdario
    Sep 4 '15 at 13:44












  • It's not a necessity. If you're using mongoose, chances are, you want the mongoose document.
    – cdbajorin
    Sep 4 '15 at 13:46






  • 1




    Sorry to be so insisting I want to fully understand; In the linked example mongoose object is used to access directly to document params (like person.occupation). In my case that did not work, since doc.password always returns undefined unless I apply .toObject() first.
    – jsdario
    Sep 4 '15 at 13:56








  • 1




    Try this: doc.password === doc._doc.password. That should evaluate to true. In the example you linked, when you access person.occupation it returns person._doc.occupation. This is why I don't understand your error. If doc.toObject().password makes your code work, doc.password should work as well.
    – cdbajorin
    Sep 4 '15 at 14:19










  • Thank you soo much!! This bit just helped me fix an issue that had me stuck for hours. I was trying to modify my server side rendered React app config thinking that this was a react context issue but turns out it was MongoDB.
    – Larrydx
    Nov 11 '17 at 18:43











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f32397419%2fmodel-findone-not-returning-docs-but-returning-a-wrapper-object%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
5
down vote



accepted










That's exactly the purpose of mongoose, wrapping mongo objects. It's what provides the ability to call mongoose methods on your documents. If you'd like the simple object, you can call .toObject() or use a lean query if you don't plan on using any mongoose magic on it at all. That being said, the equality check should still hold as doc.password returns doc._doc.password.






share|improve this answer

















  • 1




    Great. It worked! Thank you really. Do you know why in several tutorials and snippets –like this one in mongoose docs– the method .toObject() is not used?
    – jsdario
    Sep 4 '15 at 13:44












  • It's not a necessity. If you're using mongoose, chances are, you want the mongoose document.
    – cdbajorin
    Sep 4 '15 at 13:46






  • 1




    Sorry to be so insisting I want to fully understand; In the linked example mongoose object is used to access directly to document params (like person.occupation). In my case that did not work, since doc.password always returns undefined unless I apply .toObject() first.
    – jsdario
    Sep 4 '15 at 13:56








  • 1




    Try this: doc.password === doc._doc.password. That should evaluate to true. In the example you linked, when you access person.occupation it returns person._doc.occupation. This is why I don't understand your error. If doc.toObject().password makes your code work, doc.password should work as well.
    – cdbajorin
    Sep 4 '15 at 14:19










  • Thank you soo much!! This bit just helped me fix an issue that had me stuck for hours. I was trying to modify my server side rendered React app config thinking that this was a react context issue but turns out it was MongoDB.
    – Larrydx
    Nov 11 '17 at 18:43















up vote
5
down vote



accepted










That's exactly the purpose of mongoose, wrapping mongo objects. It's what provides the ability to call mongoose methods on your documents. If you'd like the simple object, you can call .toObject() or use a lean query if you don't plan on using any mongoose magic on it at all. That being said, the equality check should still hold as doc.password returns doc._doc.password.






share|improve this answer

















  • 1




    Great. It worked! Thank you really. Do you know why in several tutorials and snippets –like this one in mongoose docs– the method .toObject() is not used?
    – jsdario
    Sep 4 '15 at 13:44












  • It's not a necessity. If you're using mongoose, chances are, you want the mongoose document.
    – cdbajorin
    Sep 4 '15 at 13:46






  • 1




    Sorry to be so insisting I want to fully understand; In the linked example mongoose object is used to access directly to document params (like person.occupation). In my case that did not work, since doc.password always returns undefined unless I apply .toObject() first.
    – jsdario
    Sep 4 '15 at 13:56








  • 1




    Try this: doc.password === doc._doc.password. That should evaluate to true. In the example you linked, when you access person.occupation it returns person._doc.occupation. This is why I don't understand your error. If doc.toObject().password makes your code work, doc.password should work as well.
    – cdbajorin
    Sep 4 '15 at 14:19










  • Thank you soo much!! This bit just helped me fix an issue that had me stuck for hours. I was trying to modify my server side rendered React app config thinking that this was a react context issue but turns out it was MongoDB.
    – Larrydx
    Nov 11 '17 at 18:43













up vote
5
down vote



accepted







up vote
5
down vote



accepted






That's exactly the purpose of mongoose, wrapping mongo objects. It's what provides the ability to call mongoose methods on your documents. If you'd like the simple object, you can call .toObject() or use a lean query if you don't plan on using any mongoose magic on it at all. That being said, the equality check should still hold as doc.password returns doc._doc.password.






share|improve this answer












That's exactly the purpose of mongoose, wrapping mongo objects. It's what provides the ability to call mongoose methods on your documents. If you'd like the simple object, you can call .toObject() or use a lean query if you don't plan on using any mongoose magic on it at all. That being said, the equality check should still hold as doc.password returns doc._doc.password.







share|improve this answer












share|improve this answer



share|improve this answer










answered Sep 4 '15 at 12:58









cdbajorin

3,73811025




3,73811025








  • 1




    Great. It worked! Thank you really. Do you know why in several tutorials and snippets –like this one in mongoose docs– the method .toObject() is not used?
    – jsdario
    Sep 4 '15 at 13:44












  • It's not a necessity. If you're using mongoose, chances are, you want the mongoose document.
    – cdbajorin
    Sep 4 '15 at 13:46






  • 1




    Sorry to be so insisting I want to fully understand; In the linked example mongoose object is used to access directly to document params (like person.occupation). In my case that did not work, since doc.password always returns undefined unless I apply .toObject() first.
    – jsdario
    Sep 4 '15 at 13:56








  • 1




    Try this: doc.password === doc._doc.password. That should evaluate to true. In the example you linked, when you access person.occupation it returns person._doc.occupation. This is why I don't understand your error. If doc.toObject().password makes your code work, doc.password should work as well.
    – cdbajorin
    Sep 4 '15 at 14:19










  • Thank you soo much!! This bit just helped me fix an issue that had me stuck for hours. I was trying to modify my server side rendered React app config thinking that this was a react context issue but turns out it was MongoDB.
    – Larrydx
    Nov 11 '17 at 18:43














  • 1




    Great. It worked! Thank you really. Do you know why in several tutorials and snippets –like this one in mongoose docs– the method .toObject() is not used?
    – jsdario
    Sep 4 '15 at 13:44












  • It's not a necessity. If you're using mongoose, chances are, you want the mongoose document.
    – cdbajorin
    Sep 4 '15 at 13:46






  • 1




    Sorry to be so insisting I want to fully understand; In the linked example mongoose object is used to access directly to document params (like person.occupation). In my case that did not work, since doc.password always returns undefined unless I apply .toObject() first.
    – jsdario
    Sep 4 '15 at 13:56








  • 1




    Try this: doc.password === doc._doc.password. That should evaluate to true. In the example you linked, when you access person.occupation it returns person._doc.occupation. This is why I don't understand your error. If doc.toObject().password makes your code work, doc.password should work as well.
    – cdbajorin
    Sep 4 '15 at 14:19










  • Thank you soo much!! This bit just helped me fix an issue that had me stuck for hours. I was trying to modify my server side rendered React app config thinking that this was a react context issue but turns out it was MongoDB.
    – Larrydx
    Nov 11 '17 at 18:43








1




1




Great. It worked! Thank you really. Do you know why in several tutorials and snippets –like this one in mongoose docs– the method .toObject() is not used?
– jsdario
Sep 4 '15 at 13:44






Great. It worked! Thank you really. Do you know why in several tutorials and snippets –like this one in mongoose docs– the method .toObject() is not used?
– jsdario
Sep 4 '15 at 13:44














It's not a necessity. If you're using mongoose, chances are, you want the mongoose document.
– cdbajorin
Sep 4 '15 at 13:46




It's not a necessity. If you're using mongoose, chances are, you want the mongoose document.
– cdbajorin
Sep 4 '15 at 13:46




1




1




Sorry to be so insisting I want to fully understand; In the linked example mongoose object is used to access directly to document params (like person.occupation). In my case that did not work, since doc.password always returns undefined unless I apply .toObject() first.
– jsdario
Sep 4 '15 at 13:56






Sorry to be so insisting I want to fully understand; In the linked example mongoose object is used to access directly to document params (like person.occupation). In my case that did not work, since doc.password always returns undefined unless I apply .toObject() first.
– jsdario
Sep 4 '15 at 13:56






1




1




Try this: doc.password === doc._doc.password. That should evaluate to true. In the example you linked, when you access person.occupation it returns person._doc.occupation. This is why I don't understand your error. If doc.toObject().password makes your code work, doc.password should work as well.
– cdbajorin
Sep 4 '15 at 14:19




Try this: doc.password === doc._doc.password. That should evaluate to true. In the example you linked, when you access person.occupation it returns person._doc.occupation. This is why I don't understand your error. If doc.toObject().password makes your code work, doc.password should work as well.
– cdbajorin
Sep 4 '15 at 14:19












Thank you soo much!! This bit just helped me fix an issue that had me stuck for hours. I was trying to modify my server side rendered React app config thinking that this was a react context issue but turns out it was MongoDB.
– Larrydx
Nov 11 '17 at 18:43




Thank you soo much!! This bit just helped me fix an issue that had me stuck for hours. I was trying to modify my server side rendered React app config thinking that this was a react context issue but turns out it was MongoDB.
– Larrydx
Nov 11 '17 at 18:43


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f32397419%2fmodel-findone-not-returning-docs-but-returning-a-wrapper-object%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to change which sound is reproduced for terminal bell?

Can I use Tabulator js library in my java Spring + Thymeleaf project?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents