Can't access keys in a json that is returned from mongoose fineOne() [duplicate]
This question already has an answer here:
Can't access object property of a Mongoose response
2 answers
I am encountering a weird issue.
I have searched and found a document in my mongoDB using mongoose by using model.findOne() like so:
Model.findOne({
ID: ID
}).then(existingDoc => {
console.log(existingDoc );
res.send(existingDoc );
});
Now, everything works until now, it sends the json I expected to get. The looks like so:
{
"_id": "5bf388cf170a974770c5c942",
"ID": "11/2018",
"date": "2018-11-20T04:08:47.997Z",
"total": {
"total_market_cap": [
64301.06256298704
]
}
}
The problem is that when I try to access these values for example:
console.log(existingDoc.total);
I get undefined. Tried also using:
console.log(existingDoc['total']);
And I still get undefined.
It returned undefined for everything except the _id and __v. like it is an empty object, although it is not.
javascript node.js json mongodb mongoose
marked as duplicate by Neil Lunn
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 '18 at 20:18
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Can't access object property of a Mongoose response
2 answers
I am encountering a weird issue.
I have searched and found a document in my mongoDB using mongoose by using model.findOne() like so:
Model.findOne({
ID: ID
}).then(existingDoc => {
console.log(existingDoc );
res.send(existingDoc );
});
Now, everything works until now, it sends the json I expected to get. The looks like so:
{
"_id": "5bf388cf170a974770c5c942",
"ID": "11/2018",
"date": "2018-11-20T04:08:47.997Z",
"total": {
"total_market_cap": [
64301.06256298704
]
}
}
The problem is that when I try to access these values for example:
console.log(existingDoc.total);
I get undefined. Tried also using:
console.log(existingDoc['total']);
And I still get undefined.
It returned undefined for everything except the _id and __v. like it is an empty object, although it is not.
javascript node.js json mongodb mongoose
marked as duplicate by Neil Lunn
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 '18 at 20:18
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Can't access object property of a Mongoose response
2 answers
I am encountering a weird issue.
I have searched and found a document in my mongoDB using mongoose by using model.findOne() like so:
Model.findOne({
ID: ID
}).then(existingDoc => {
console.log(existingDoc );
res.send(existingDoc );
});
Now, everything works until now, it sends the json I expected to get. The looks like so:
{
"_id": "5bf388cf170a974770c5c942",
"ID": "11/2018",
"date": "2018-11-20T04:08:47.997Z",
"total": {
"total_market_cap": [
64301.06256298704
]
}
}
The problem is that when I try to access these values for example:
console.log(existingDoc.total);
I get undefined. Tried also using:
console.log(existingDoc['total']);
And I still get undefined.
It returned undefined for everything except the _id and __v. like it is an empty object, although it is not.
javascript node.js json mongodb mongoose
This question already has an answer here:
Can't access object property of a Mongoose response
2 answers
I am encountering a weird issue.
I have searched and found a document in my mongoDB using mongoose by using model.findOne() like so:
Model.findOne({
ID: ID
}).then(existingDoc => {
console.log(existingDoc );
res.send(existingDoc );
});
Now, everything works until now, it sends the json I expected to get. The looks like so:
{
"_id": "5bf388cf170a974770c5c942",
"ID": "11/2018",
"date": "2018-11-20T04:08:47.997Z",
"total": {
"total_market_cap": [
64301.06256298704
]
}
}
The problem is that when I try to access these values for example:
console.log(existingDoc.total);
I get undefined. Tried also using:
console.log(existingDoc['total']);
And I still get undefined.
It returned undefined for everything except the _id and __v. like it is an empty object, although it is not.
This question already has an answer here:
Can't access object property of a Mongoose response
2 answers
javascript node.js json mongodb mongoose
javascript node.js json mongodb mongoose
edited Nov 20 '18 at 22:39
Randomizer
asked Nov 20 '18 at 18:33
RandomizerRandomizer
10010
10010
marked as duplicate by Neil Lunn
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 '18 at 20:18
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Neil Lunn
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 '18 at 20:18
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Can you try to convert it toObject
Model.findOne({
ID: ID
}).then(existingDoc => {
console.log(existingDoc );
let newdoc = existingDoc.toObject();
console.log(newdoc.myProperty)
res.send(existingDoc );
});
It worked! although I don't know why. when I used typeof on the existingDoc it was an object already. Thanks!
– Randomizer
Nov 20 '18 at 20:00
Could you add more explanation about this behavior? it looks interesting.
– Samuel
Nov 20 '18 at 20:13
Mongoose documentation says that this method converts a document to a plain js object. After having a plan js object, we can iterate the properties. mongoosejs.com/docs/api.html#document_Document-toObject
– shmit
Nov 20 '18 at 20:16
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Can you try to convert it toObject
Model.findOne({
ID: ID
}).then(existingDoc => {
console.log(existingDoc );
let newdoc = existingDoc.toObject();
console.log(newdoc.myProperty)
res.send(existingDoc );
});
It worked! although I don't know why. when I used typeof on the existingDoc it was an object already. Thanks!
– Randomizer
Nov 20 '18 at 20:00
Could you add more explanation about this behavior? it looks interesting.
– Samuel
Nov 20 '18 at 20:13
Mongoose documentation says that this method converts a document to a plain js object. After having a plan js object, we can iterate the properties. mongoosejs.com/docs/api.html#document_Document-toObject
– shmit
Nov 20 '18 at 20:16
add a comment |
Can you try to convert it toObject
Model.findOne({
ID: ID
}).then(existingDoc => {
console.log(existingDoc );
let newdoc = existingDoc.toObject();
console.log(newdoc.myProperty)
res.send(existingDoc );
});
It worked! although I don't know why. when I used typeof on the existingDoc it was an object already. Thanks!
– Randomizer
Nov 20 '18 at 20:00
Could you add more explanation about this behavior? it looks interesting.
– Samuel
Nov 20 '18 at 20:13
Mongoose documentation says that this method converts a document to a plain js object. After having a plan js object, we can iterate the properties. mongoosejs.com/docs/api.html#document_Document-toObject
– shmit
Nov 20 '18 at 20:16
add a comment |
Can you try to convert it toObject
Model.findOne({
ID: ID
}).then(existingDoc => {
console.log(existingDoc );
let newdoc = existingDoc.toObject();
console.log(newdoc.myProperty)
res.send(existingDoc );
});
Can you try to convert it toObject
Model.findOne({
ID: ID
}).then(existingDoc => {
console.log(existingDoc );
let newdoc = existingDoc.toObject();
console.log(newdoc.myProperty)
res.send(existingDoc );
});
Model.findOne({
ID: ID
}).then(existingDoc => {
console.log(existingDoc );
let newdoc = existingDoc.toObject();
console.log(newdoc.myProperty)
res.send(existingDoc );
});
Model.findOne({
ID: ID
}).then(existingDoc => {
console.log(existingDoc );
let newdoc = existingDoc.toObject();
console.log(newdoc.myProperty)
res.send(existingDoc );
});
answered Nov 20 '18 at 19:57
shmitshmit
507310
507310
It worked! although I don't know why. when I used typeof on the existingDoc it was an object already. Thanks!
– Randomizer
Nov 20 '18 at 20:00
Could you add more explanation about this behavior? it looks interesting.
– Samuel
Nov 20 '18 at 20:13
Mongoose documentation says that this method converts a document to a plain js object. After having a plan js object, we can iterate the properties. mongoosejs.com/docs/api.html#document_Document-toObject
– shmit
Nov 20 '18 at 20:16
add a comment |
It worked! although I don't know why. when I used typeof on the existingDoc it was an object already. Thanks!
– Randomizer
Nov 20 '18 at 20:00
Could you add more explanation about this behavior? it looks interesting.
– Samuel
Nov 20 '18 at 20:13
Mongoose documentation says that this method converts a document to a plain js object. After having a plan js object, we can iterate the properties. mongoosejs.com/docs/api.html#document_Document-toObject
– shmit
Nov 20 '18 at 20:16
It worked! although I don't know why. when I used typeof on the existingDoc it was an object already. Thanks!
– Randomizer
Nov 20 '18 at 20:00
It worked! although I don't know why. when I used typeof on the existingDoc it was an object already. Thanks!
– Randomizer
Nov 20 '18 at 20:00
Could you add more explanation about this behavior? it looks interesting.
– Samuel
Nov 20 '18 at 20:13
Could you add more explanation about this behavior? it looks interesting.
– Samuel
Nov 20 '18 at 20:13
Mongoose documentation says that this method converts a document to a plain js object. After having a plan js object, we can iterate the properties. mongoosejs.com/docs/api.html#document_Document-toObject
– shmit
Nov 20 '18 at 20:16
Mongoose documentation says that this method converts a document to a plain js object. After having a plan js object, we can iterate the properties. mongoosejs.com/docs/api.html#document_Document-toObject
– shmit
Nov 20 '18 at 20:16
add a comment |