Sails.js - 如何更新嵌套模型 [英] Sails.js - how to update nested model

查看:33
本文介绍了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'
}

我目前注册了用户,然后在注册后更新个人资料信息.如何将配置文件数据添加到此模型?

I currently register the user then update profile information post-registration. How to I go about adding the profile data to this model?

我在别处读到 push 方法应该有效,但它没有.我收到这个错误:TypeError: Object [object Object] has no method 'push'

I read elsewhere that the push method should work, but it doesn't. I get this error: TypeError: Object [object Object] has no method '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 是正确的.Sails 仅支持以下模型属性类型

@Zolmeister is correct. Sails only supports the following model attribute types

字符串、文本、整数、浮点数、日期、时间、日期时间、布尔值、二进制、数组、json

它们也不支持关联(否则在这种情况下会很有用)

They also do not support associations (which would otherwise be useful in this case)

GitHub 问题 #124.

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

You can get around this by bypassing sails and using mongo's native methods like such:

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 消息等)

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

这篇关于Sails.js - 如何更新嵌套模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆