0

我对 CompoundJS 和模型关系有疑问。我尝试在 Creation 和 Photo 之间建立关系,它之所以有效,是因为我能够在 Photo 表中看到“creationId”。但是,现在我想在我用来显示所有创作的视图中获取每个创作的所有照片。可能吗?我该怎么办?

我的模型:

var Creation = describe('Creation', function () {
    property('title', String);
    property('description', Text);
    property('thumbnail', Text);
    property('moodboard', String);
    property('date', Date);
    property('location', String);
    set('restPath', pathTo.creations);
});

var Photo = describe('Photo', function () {
    property('name', String);
    property('file', String);
    set('restPath', pathTo.photos);
});

Creation.hasMany(Photo, {as: 'photos', foreignKey: 'creationId'});
Photo.belongsTo(Creation, {as: 'creation', foreignKey: 'creationId'});

我的创作控制器:

action(function index() {
    this.title = 'Creations index';
    Creation.all(function (err, creations) {
        console.log(creations);
        switch (params.format) {
            case "json":
                send({code: 200, data: creations});
                break;
            default:
                render({
                    creations: creations
                });
        }
    });
});

当我添加照片时:

    Photo.create(req.body.Photo, function (err, photo) {
        respondTo(function (format) {
            format.json(function () {
                if (err) {
                    send({code: 500, error: photo && photo.errors || err});
                } else {
                    send({code: 200, data: photo.toObject()});
                }
            });
            format.html(function () {
                if (err) {
                    flash('error', 'Photo can not be created');
                    render('new', {
                        photo: photo,
                        title: 'New photo',
                        layout: 'photos'
                    });
                } else {
                    flash('info', 'Photo created');
                    redirect(path_to.photos);
                }
            });
        });
    });
});
4

0 回答 0