如何在sails.js中查看完整的相关模型?

时间:2015-04-25 16:21:39

标签: orm sails.js one-to-many waterline sails-mongo

我已经创建了一个sails.js应用程序。

我正在尝试在视图中获得完整的相关模型Creator,但我只得到id ...

我该怎么做?

我这样做:

Model Author.js

    module.exports = {

        attributes: {
            name: {
                type: 'string',
                required: true
            },
            description: {
                type: 'string'
            },
            history: {
                type: 'string'
            },

            creator: {
                model: 'User'
            }
        }
    };

模型User.js

    module.exports = {


    attributes: {
        name: {
            type: 'string',
            required: true
        },
        email: {
            type: 'string',
            email: true,
            required: true,
            unique: true
        }
        authors: {
            collection: 'Author',
            via: 'creator'
        },
...
    }
};

AuthorController:

show: function(req, res, next) {
        Author.findOne(req.param('id'), function foundAuthor(err, author) {
            console.log(author.creator);
            res.view({
                author: author,
            });
        });

    }

在我看来作者/ show.ejs

<div class="container">

    <h1><%= author.name %></h1>
    <h3><%= author.creator %></h3>
    <h3><%= author.creator.name %></h3>


</div>

author.creator.name未定义 和author.creator是id

如何在作者视图中获取完整模型用户ID?

1 个答案:

答案 0 :(得分:1)

你需要告诉帆填充创作者

show: function(req, res, next) {
        Author.findOne(req.param('id').populate('creator').exec(function foundAuthor(err, author) {
            console.log(author.creator);
            res.view({
                author: author,
            });
        });
    }