Sailsjs "Error: There was an error turning the model into an object."

sailsjs 0.11, node 0.12.4. with a passport 0.2.1 and a local passport 1.0

Using sailsjs project routes only to find out what is going on. The project is a simple blog, and I tried to view the user profile in the project route /user/4 to show me userid 4. This message was the result.

Any ideas what caused this?

Note: I was not anywhere near the source of the sails / waterline.

Full error message

 /home/vagrant/nodeprojs/<project>/node_modules/sails/node_modules/waterline/lib/waterline/model/lib/defaultMethods/toObject.js:59 throw err; ^ Error: There was an error turning the model into an object. at new module.exports (/home/vagrant/nodeprojs/<project>/node_modules/sails/node_modules/waterline/lib/waterline/model/lib/defaultMethods/toObject.js:56:15) at prototypeFns.toObject (/home/vagrant/nodeprojs/<project>/node_modules/sails/node_modules/waterline/lib/waterline/model/index.js:30:14) at prototypeFns.toJSON (/home/vagrant/nodeprojs/<project>/node_modules/sails/node_modules/waterline/lib/waterline/model/index.js:112:20) at Object.stringify (native) at ServerResponse.res.json (/home/vagrant/nodeprojs/<project>/node_modules/sails/node_modules/express/lib/response.js:217:19) at sendJSON (/home/vagrant/nodeprojs/<project>/api/responses/ok.js:34:23) at viewReady (/home/vagrant/nodeprojs/<project>/api/responses/ok.js:72:25) at viewFailedToRender (/home/vagrant/nodeprojs/<project>/node_modules/sails/lib/hooks/views/res.view.js:276:16) at Function.app.render (/home/vagrant/nodeprojs/<project>/node_modules/sails/node_modules/express/lib/application.js:514:14) at ServerResponse.res.render (/home/vagrant/nodeprojs/<project>/node_modules/sails/node_modules/express/lib/response.js:827:7) at ServerResponse.res.view (/home/vagrant/nodeprojs/<project>/node_modules/sails/lib/hooks/views/res.view.js:237:16) at Object.sendOK (/home/vagrant/nodeprojs/<project>/api/responses/ok.js:71:19) at ServerResponse.bound [as ok] (/home/vagrant/nodeprojs/<project>/node_modules/sails/node_modules/lodash/dist/lodash.js:729:21) at found (/home/vagrant/nodeprojs/<project>/node_modules/sails/lib/hooks/blueprints/actions/findOne.js:37:9) at wrapper (/home/vagrant/nodeprojs/<project>/node_modules/sails/node_modules/waterline/node_modules/lodash/index.js:3602:19) at applyInOriginalCtx (/home/vagrant/nodeprojs/<project>/node_modules/sails/node_modules/waterline/lib/waterline/utils/normalize.js:421:80) 

And the User.js. model toJSON is commented out because the message originally pointed to this section. After commenting, this line 3 ( at User.attributes.toJSON ) is now different.

 var bcrypt = require ('bcrypt') var User = { attributes: { email: { type: 'email', required: true, unique: true }, password: { type: 'string', minLength: 6, required: true }, admin: { type: 'boolean', defaultsTo: false // separate from acl }, acl: { type: 'string', defaultsTo: 'USER' // MOD, USER (unlogged should get GUEST) } /* toJSON: function() { var obj = this.toObject(); delete obj.password; return obj; } */ }, beforeCreate: function(user, cb) { bcrypt.genSalt(10, function(err, salt) { bcrypt.hash(user.password, salt, function(err, hash) { if (err) { console.log(err); cb(err); } else { user.password = hash; cb(); } }); }); } }; module.exports = User; 

[$ E] After a further review, I performed an object-string conversion and displayed it at several points. The object in the first line of the try and the first line of the sequenceestn catch expression:

  waterline:toObject > this.object TRY= >>> email: admin@admin.com password: $2a$10$bNktAvahfa3f8Q8lTV262u9RrLUqdvp7SzfM6dQqki3YliXdLMWxK admin: true acl: USER createdAt: Fri May 29 2015 23:25:35 GMT+0000 (UTC) updatedAt: Fri May 29 2015 23:25:35 GMT+0000 (UTC) id: 1 _locals: [object Object] <<< +2ms waterline:toObject > this.object CATCH= >>> email: admin@admin.com password: $2a$10$bNktAvahfa3f8Q8lTV262u9RrLUqdvp7SzfM6dQqki3YliXdLMWxK admin: true acl: USER createdAt: Fri May 29 2015 23:25:35 GMT+0000 (UTC) updatedAt: Fri May 29 2015 23:25:35 GMT+0000 (UTC) id: 1 _locals: [object Object] <<< 

(this output should not be json, it's just a dump)

If someone doesn’t have a better idea, I think I’ll just move on to the sailsjs tracker.

+5
source share
1 answer

I had this problem and after huge problems I was finally able to fix it (I think now it took me so long to fix this error as soon as it took to create the application in the first place ...)

Without seeing your controller code, I don’t know if this will fix it for you, but for me the problem with toJSON seems to occur when submitting the User model to partial. I found that explicitly providing the JSON model before passing it to the rendering function fixed the problem.

In other words, I changed this:
 res.render('partials/user-card', user, function(err, html){ partialData = user.toJSON(); partialData.html = html; // Send out a message (with HTML fragment) to socket subscribers User.publishUpdate(user.id, partialData); }); 

... to that:

 partialData = user.toJSON(); res.render('partials/user-card', partialData, function(err, html){ partialData.html = html; // Send out a message (with HTML fragment) to socket subscribers User.publishUpdate(user.id, partialData); }); 

(i.e. move user.toJSON from the render function callback and submit the JSON, not the model itself, to the render function). This allowed me to save the toJSON password removal method.

0
source

All Articles