Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defined variable is undefined

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?

like image 704
Last Rose Studios Avatar asked Aug 18 '12 12:08

Last Rose Studios


People also ask

What does it mean when a variable is undefined?

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.

How do you know if a variable is undefined?

So the correct way to test undefined variable or property is using the typeof operator, like this: if(typeof myVar === 'undefined') .

How do you solve an undefined variable?

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 ...

Can you set a variable to undefined?

YES, you can, because undefined is defined as undefined.


1 Answers

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();

like image 123
Vadim Baryshev Avatar answered Sep 22 '22 20:09

Vadim Baryshev