Skip to content

Instantly share code, notes, and snippets.

@tauren
Created March 13, 2014 08:03
Show Gist options
  • Save tauren/9523951 to your computer and use it in GitHub Desktop.
Save tauren/9523951 to your computer and use it in GitHub Desktop.
It seems that toJSON is being called on the results of another toJSON call.
STEP 1
npm install sails@beta -g
git clone git@github.com:coinprofit/sails-model-question.git
cd sails-model-question
npm init
sails lift
STEP 2
Hit this URL in a browser:
http://localhost:1337/user/create?username=foo&password=foo&email=foo@foo.com
STEP 3
Review server output. Note that in the 2nd call, the this object in toJSON() does not contain email or password.
In toJSON { username: 'foo',
password: 'foo',
email: 'foo@foo.com',
createdAt: Thu Mar 13 2014 00:58:28 GMT-0700 (PDT),
updatedAt: Thu Mar 13 2014 00:58:28 GMT-0700 (PDT),
id: 1 }
In toJSON { username: 'foo',
createdAt: Thu Mar 13 2014 00:58:28 GMT-0700 (PDT),
updatedAt: Thu Mar 13 2014 00:58:28 GMT-0700 (PDT),
id: 1,
avatar: 'https://secure.gravatar.com/avatar/3717483f26171b61a4e2154fb37ffbd1?s=25&r=pg&d=mm' }
error: Server Error (500)
error: TypeError: Cannot call method 'toLowerCase' of undefined
at Object.module.exports.url (/Users/tauren/github/coinprofit/sails-model-question/node_modules/gravatar/lib/gravatar.js:10:62)
at module.exports.attributes.gravatarImage (/Users/tauren/github/coinprofit/sails-model-question/api/models/User.js:30:23)
at module.exports.attributes.toJSON (/Users/tauren/github/coinprofit/sails-model-question/api/models/User.js:36:26)
at Object.stringify (native)
at ServerResponse.res.json (/Users/tauren/github/coinprofit/sails-model-question/node_modules/sails/node_modules/express/lib/response.js:189:19)
at created (/Users/tauren/github/coinprofit/sails-model-question/node_modules/sails/lib/hooks/blueprints/actions/create.js:71:15)
at _normalizeCallback.callback.success (/Users/tauren/github/coinprofit/sails-model-question/node_modules/sails/node_modules/waterline/node_modules/node-switchback/lib/normalize.js:33:26)
at _switch (/Users/tauren/github/coinprofit/sails-model-question/node_modules/sails/node_modules/waterline/node_modules/node-switchback/lib/factory.js:34:26)
at /Users/tauren/github/coinprofit/sails-model-question/node_modules/sails/node_modules/waterline/lib/waterline/query/dql.js:106:11
at /Users/tauren/github/coinprofit/sails-model-question/node_modules/sails/node_modules/async/lib/async.js:142:25
@jDmacD
Copy link

jDmacD commented Apr 14, 2014

hi, I've encountered the same problem. Did you find a fix or a workaround? Thanks in advance.

@lfsmoura
Copy link

I've got the same problem. If your user model is contained in another model, the other model should overwrite toJSON too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment