Node.js 帆在两个模型中插入相同的对象
是否可以在两个模型中插入两个相等的对象?? 我创建了两个模型Playlist和Song,我创建了一个song1和两个Playlist:playlist1和playlist2;我希望playl1有一首歌,playl2有一首歌(歌1) 但是,当我将播放列表1中的歌曲1与播放列表2中的歌曲1关联时,播放列表1中的关系就会出现。 这个关系是1对N,我试过用一个N对N的关系,但我不能解决它 有人能帮我吗??:(Node.js 帆在两个模型中插入相同的对象,node.js,mongodb,sails.js,Node.js,Mongodb,Sails.js,是否可以在两个模型中插入两个相等的对象?? 我创建了两个模型Playlist和Song,我创建了一个song1和两个Playlist:playlist1和playlist2;我希望playl1有一首歌,playl2有一首歌(歌1) 但是,当我将播放列表1中的歌曲1与播放列表2中的歌曲1关联时,播放列表1中的关系就会出现。 这个关系是1对N,我试过用一个N对N的关系,但我不能解决它 有人能帮我吗??:( 对不起,我的问题是,在控制器中,我有主模型,并且使用两个模型 主要型号: --- Playl
对不起,我的问题是,在控制器中,我有主模型,并且使用两个模型 主要型号:
--- Playlist.js ---
module.exports = {
attributes: {
name: {
type: 'string',
required: true,
unique: true
},
// Associations
tablets_playlists: {
collection: 'Tablet',
via: 'lista_tablet'
},
songs_playlist: {
collection: 'Song',
via: 'song'
}
}
};
其他型号:
--- Song.js ---
module.exports = {
attributes: {
group: {
type: 'string',
required: true,
unique: true
},
song: {
type: 'string'
},
// Association
song: {
collection: 'Playlist',
via: 'songs_playlist'
}
}
};
--- Tablet.js ---
module.exports = {
attributes: {
MAC: {
type: 'string',
required: true,
unique: true
},
// Associations
lista_tablet: {
collection: 'Playlist',
via: 'tablets_playlists'
}
};
当我在渲染视图中将歌曲与播放列表或平板电脑与播放列表关联时,不会显示。但如果关系为1到N,则会显示。当我将同一首歌曲关联两次时,服务器崩溃,我认为这是因为bbdd中存在或试图添加两个相等的对象
这是我将平板电脑与播放列表关联的方法,类似于歌曲(我以前发布过)
这是PlayController中要显示的代码,用于渲染视图:
// render the profile view (e.g. /playlist/show/.ejs)
show: function(req, res, next) {
Playlist.findOne(req.param('id'), function foundPlaylist(err, playlist) {
if (err) return next(err);
if (!playlist) return next();
Tablet.find({'listas_reproduccion':playlist.id},function foundTablets(err, tablets) {
if (err) return next(err);
if (!tablets) return next();
Song.find({'song':playlist.id}, function foundSongs(err, songs) {
if (err) return next(err);
if (!songs) return next();
res.view({
songs: songs,
tablets: tablets,
playlist: playlist
});
});
});
});
},
看起来你有一对一的关系,但你需要多对多 歌曲有多个播放列表,播放列表有多首歌曲。您必须通过属性给出: 歌曲型号: 播放列表模式:
我想你需要发布相关的型号和控制器来破解你的问题。如果你看到我的代码,可能是我更新时更改了关联,但是我如何才能在一个型号中插入两个相等的对象?我希望有两个播放列表具有相同的歌曲
Playlist.song.add(song_id);Playlist.save();
Sails.js上有一个例子,非常感谢您的帮助!我完成了这个项目:D
// render the profile view (e.g. /playlist/show/.ejs)
show: function(req, res, next) {
Playlist.findOne(req.param('id'), function foundPlaylist(err, playlist) {
if (err) return next(err);
if (!playlist) return next();
Tablet.find({'listas_reproduccion':playlist.id},function foundTablets(err, tablets) {
if (err) return next(err);
if (!tablets) return next();
Song.find({'song':playlist.id}, function foundSongs(err, songs) {
if (err) return next(err);
if (!songs) return next();
res.view({
songs: songs,
tablets: tablets,
playlist: playlist
});
});
});
});
},
module.exports = {
attributes: {
name:'STRING',
// Add a reference to Playlists
playlists: {
collection: 'playlists',
via: 'songs'
}
}
}
module.exports = {
attributes: {
name:'STRING',
// Add a reference to Songs
songs: {
collection: 'songs',
via: 'playlists'
}
}
}