简体   繁体   English

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

[英]Sails.js - how to update nested model

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'
}

I currently register the user then update profile information post-registration. 我目前注册用户,然后在注册后更新个人资料信息。 How to I go about adding the profile data to this model? 如何将配置文件数据添加到此模型?

I read elsewhere that the push method should work, but it doesn't. 我在别处读过push方法应该有效,但事实并非如此。 I get this error: TypeError: Object [object Object] has no method 'push' 我收到此错误:TypeError:Object [object Object]没有方法'push'

        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 is correct. @Zolmeister是对的。 Sails only supports the following model attribute types Sails仅支持以下模型属性类型

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

They also do not support associations (which would otherwise be useful in this case) 它们也不支持关联(在这种情况下可能会有用)

GitHub Issue #124 . GitHub问题#124

You can get around this by bypassing sails and using mongo's native methods like such: 您可以绕过风帆并使用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

    });

});

Keep in mind that their shims are there for a reason. 请记住,他们的垫片是有原因的。 Bypassing them will remove some of the functionality that is otherwise handled behind the scenes (translating id queries to ObjectIds, sending pubsub messages via socket, etc.) 绕过它们将删除一些在幕后处理的功能(将id查询转换为ObjectIds,通过套接字发送pubsub消息等)

Currently Sails doesn't support nested model definitions (as far as I know). 目前Sails不支持嵌套模型定义(据我所知)。 You could try using the 'json' type. 您可以尝试使用'json'类型。 After that you would simply have: 在那之后你只需:

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

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

Too late to reply, but for others (as a reference), they can do something like this: 回复太晚了,但对于其他人(作为参考),他们可以这样做:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM