2013-11-05 63 views
4
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' 
} 

我目前注册用户,然后更新配置文件信息后注册。如何开始将配置文件数据添加到此模型?Sails.js - 如何更新嵌套模型

我在其他地方看过这个推送方法应该可以工作,但事实并非如此。我得到这个错误:类型错误:对象的翻译:有没有方法“推”

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

     }); 
+0

您正在使用哪个数据库? – colbydauph

+0

我正在使用MongoDB – diskodave

回答

4

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

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

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

GitHub Issue #124

您可以通过绕过帆和使用像这样的蒙戈的本地方法解决这个问题:

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 

    }); 

}); 

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

+0

不确定这是否有效 - 'collection.find'没有promise风格的'done'方法。 –

+0

它可能已在v0.10中更改。如果是这样的话,它可能仍然遵循作为.find()的最后一个参数传递回调的标准。 – colbydauph

2

目前帆不支持嵌套模型定义(据我所知)。您可以尝试使用'json'类型。 之后,你只会有:

user.profile = { 
    firstname : first, 
    lastname : last, 
    zipcode: zip 
}) 

user.save(function(error) { 
    console.log(error) 
}); 
+4

这是令人失望的是Sails是为mongoDB构建的......这是使用面向文档的数据库的优势。 – diskodave

1

太迟不能回复,但对于其他人(作为参考),他们可以这样做:

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