sailsjs“错误:将模型转换为对象时出错。”

时间:2015-06-02 19:44:59

标签: javascript node.js sails.js waterline

sailsjs 0.11,节点0.12.4。 护照0.2.1和护照本地1.0

使用sailsjs蓝图路线只是为了看看发生了什么。该项目只是一个简单的博客,我尝试在蓝图路由/user/4中查看用户配置文件,向我显示用户ID 4.结果是此消息。

是什么原因引起了这个?

注意:我没有靠近帆/水线源。

完整的错误消息

/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)

User.js模型。 toJSON已注释掉,因为该消息最初指向该部分。在评论之后,第3行(at User.attributes.toJSON)现在不同了。

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] 经过进一步的审查,我做了一个对象到字符串的转换,并在几个点输出它。 try语句第一行的对象和subsequestn catch语句的第一行是:

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

(这个输出不应该是json,它只是一个转储)

除非有人有更好的想法,否则我想我会继续前往sailsjs问题跟踪器。

1 个答案:

答案 0 :(得分:0)

我遇到了这个问题,经过巨大的麻烦之后我终于设法解决了它(我认为现在我花了很长时间才修复这个错误,因为它构建了应用程序首先......)

如果没有看到您的控制器代码,我不知道这是否会为您解决,但对我来说,将{User}模型提供给部分时似乎会发生问题toJSON。我发现在将模型传递给render函数之前,明确地将模型呈现为JSON 可以解决问题。

换句话说,我改变了这个:

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

......对此:

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

(即将user.toJSON移出render函数的回调,并将JSON而不是模型本身提供给render函数)。这样我就可以保留密码删除toJSON方法了。