Sails.js – 如何更新嵌套模型

attributes: { username: { type: 'email', // validated by the ORM required: true }, password: { type: 'string', required: true }, profile: { firstname: 'string', lastname: 'string', photo: 'string', birthdate: 'date', zipcode: 'integer' }, followers: 'array', followees: 'array', blocked: 'array' } 

我目前正在注册用户,然后更新configuration文件信息后注册。 如何去添加个人资料数据到这个模型?

我在其他地方读到推送方法应该工作,但它不。 我得到这个错误:TypeError:Object [object Object]没有方法'推'

  Users.findOne(req.session.user.id).done(function(error, user) { user.profile.push({ firstname : first, lastname : last, zipcode: zip }) user.save(function(error) { console.log(error) }); }); 

@Zolmeister是正确的。 Sails仅支持以下模型属性types

string, text, integer, float, date, time, datetime, boolean, binary, array, json

他们也不支持协会(否则在这种情况下会有用)

GitHub问题#124 。

你可以通过绕过帆并使用mongo的本地方法来解决这个问题:

 Model.native(function(err, collection){ // Handle Errors collection.find({'query': 'here'}).done(function(error, docs) { // Handle Errors // Do mongo-y things to your docs here }); }); 

请记住,他们的垫片是有原因的。 绕过它们将删除一些在幕后处理的function(将id查询转换为ObjectIds,通过套接字发送pubsub消息等)

目前Sails不支持嵌套的模型定义(据我所知)。 你可以尝试使用'json'types。 之后,你只需要:

 user.profile = { firstname : first, lastname : last, zipcode: zip }) user.save(function(error) { console.log(error) }); 

太迟了,但是对于其他人(作为参考),他们可以做这样的事情:

 Users.findOne(req.session.user.id).done(function(error, user) { profile = { firstname : first, lastname : last, zipcode: zip }; User.update({ id: req.session.user.id }, { profile: profile}, function(err, resUser) { }); });