0

Hi I'm trying to get a mongoose error message, and the description has an array index in its name. This is in a unit test.

{message: 'Validation failed',
  name: 'ValidationError',
  errors:
   { 'contactInfo.0.email':
      { message: 'Invalid email',
        name: 'ValidatorError',
        path: 'email',
        type: 'user defined',
        value: 'fjksdal' } },
  key: 'undefined.0.undefined' }

previous example to get message i would do this:

(err.errors.passHash.message).should.eql('PASSWORD_NOT_HASHED');

However variations of this fail and I can see why, not sure what the workaround is.

(err.errors.contactInfo.0.email.message).should.eql('Invalid Email')

thanks

Steve Holt
  • 85
  • 9
  • possible duplicate of [How to access object properties containing special characters?](http://stackoverflow.com/questions/12953704/how-to-access-object-properties-containing-special-characters) – Felix Kling Apr 13 '14 at 00:45
  • you are correct Felix, I was improperly asking the question to my ignorance. – Steve Holt Apr 13 '14 at 01:29

1 Answers1

0

That's not valid JSON, so I assume that you mean that it's a Javascript object.

Use bracket syntax to access properties with names that can't be identifiers:

(err.errors['contactInfo.0.email'].message).should.eql('Invalid Email')
Guffa
  • 687,336
  • 108
  • 737
  • 1,005