I am hitting up mongo (via mongoose customer.find()
) and getting back something that looks like
[{ _id: 5029a09e099fb5095fdb2d73,
clientId: 22,
company: 'X',
email: '[email protected]',
firstName: 'X',
lastName: 'Y',
quality: 'Sold',
address:
{ phone: '',
alt: '',
street1: '',
street2: '',
city: '',
state: 'Ontario',
country: 'Canada',
code: '' },
comments: []
}]
Please note that this is the result from console.log directly. This is not something I am entering, this is the results from mongoDB. _id is being returned in this form by mongo and has absolutely nothing to do with the issue.
when I try
console.log(customer[0])
I get
{ _id: 5029a09e099fb5095fdb2d73,
clientId: 22,
company: 'X',
email: '[email protected]',
firstName: 'X',
lastName: 'Y',
quality: 'Sold',
address:
{ phone: '',
alt: '',
street1: '',
street2: '',
city: '',
state: 'Ontario',
country: 'Canada',
code: '' },
comments: []
}
as expected
when I try console.log(customer[0].quality)
I get undefined
when I try console.log(customer[0].email)
or any of the others it works fine, and I get the expected value.
to my knowledge, quality isn't a reserved word, am I missing something?
An undefined variable is a variable used in a program that was not previously declared in the source code. In most programming languages, this results in an error.
So the correct way to test undefined variable or property is using the typeof operator, like this: if(typeof myVar === 'undefined') .
There are a few ways to fix an undefined variable: (1) You can rename the variable or variable set so that it matches what you have used in the topic; (2) you can re-insert the variable in the topic so that it uses an existing variable set/variable name, (3) you can add the undefined variable to the project as a new ...
YES, you can, because undefined is defined as undefined.
Mongoose find returns Query object. It not just plain javascript object. It inherits methods of your model, query methods, methods of Document. console.log shows you just toString method result of this object. It may be modified by getters or virtuals. _id without quotes also toString result of ObjectId class. Try access your value by customer[0].get('quality'); Or convert it to plain object via customer[0].toObject();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With