【问题标题】:How can i get the full related model in view in sails.js?如何在sails.js 中查看完整的相关模型?
【发布时间】:2015-07-04 06:02:50
【问题描述】:

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

我正在尝试查看完整的相关模型 Creator,但我只得到 id...

我该怎么做?

我正在这样做:

模型作者.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'
        },
...
    }
};

作者控制器:

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?

【问题讨论】:

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


    【解决方案1】:

    您需要告诉sails 填充创建者

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

    【讨论】:

    • 没错!非常感谢!但是您需要在示例中的“req.param('id')”之后设置“)”。
    猜你喜欢
    • 1970-01-01
    • 2015-05-11
    • 1970-01-01
    • 1970-01-01
    • 2018-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多