gpt4 book ai didi

javascript - sails 船 "Error: There was an error turning the model into an object."

转载 作者:搜寻专家 更新时间:2023-10-31 22:54:48 26 4
gpt4 key购买 nike

sailsjs 0.11, Node 0.12.4。使用护照 0.2.1 和本地护照 1.0

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

知道是什么原因造成的吗?

注意:我没有去过 sails/水线源附近的任何地方。

完整的错误信息

/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 问题跟踪器。

最佳答案

我遇到了这个问题,在经历了巨大的麻烦后,我终于设法修复了它(我想我现在修复这个错误所花费的时间与构建应用程序所花费的时间一样长首先……)

没有看到您的 Controller 代码,我不知道这是否会为您解决问题,但对我来说,toJSON 的问题似乎发生在将 User 模型提供给 partial 时。我发现在将模型传递给渲染函数之前显式地将模型渲染为 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 移出渲染函数的回调,并将 JSON 而不是模型本身提供给渲染函数)。这让我也可以保留删除密码的 toJSON 方法。

关于javascript - sails 船 "Error: There was an error turning the model into an object.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30605228/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com