我试图创建专辑曲目,但我遇到了一个错误
这是我的路线代码
// Adding track to album
router.get('/:album_id/track/new', function(req, res){
Album.findById(req.params.album_id, function(err, falbum){
if(err){
console.log(err);
} else {
res.render('tracks/add', {albums: falbum});
}
});
});
// Post routes
router.post('/:album_id/track',upload.single('songs'), function(req, res){
Album.findById(req.params.album_id, function(err, album){
if(err){
console.log(err);
} else {
if(req.file){
console.log('Uploading Song ....');
var songs = req.file.filename;
} else {
console.log('No Song Uploaded...');
}
var songname = req.body.songname;
var songs = songs;
var newSong = {songname: songname, songs:songs};
Track.create(newSong, function(err, track){
if(err){
console.log(err);
} else {
track.albumId.id = req.params.album_id;
track.save();
album.tracks.push(track);
album.save();
console.log('New Track is created');
res.redirect('/albums/' + album._id);
}
});
}
});
});
router.get('/:album_id',function(req, res){
Album.findById(req.params.album_id).populate('tracks').exec(function(err, album){
if(err){
console.log(err);
} else {
res.render('albums/show', {albums: album})
}
});
});
module.exports = router;
我得到以下错误
v4\node_modules\mongoose\node_modules\mongodb\lib\utils.js:98
process.nextTick(function() { throw err; });
^
TypeError: Cannot read property 'tracks' of null
at C:\Users\tjesu\Desktop\pidal\pidal-v4\routes\albums.js:53:11
at Function.<anonymous> (C:\Users\tjesu\Desktop\pidal\pidal-v4\node_modules\mongoose\lib\model.js:3324:16)
at C:\Users\tjesu\Desktop\pidal\pidal-v4\node_modules\mongoose\lib\model.js:1870:14
at C:\Users\tjesu\Desktop\pidal\pidal-v4\node_modules\async\lib\async.js:726:13
at C:\Users\tjesu\Desktop\pidal\pidal-v4\node_modules\async\lib\async.js:52:16
at done (C:\Users\tjesu\Desktop\pidal\pidal-v4\node_modules\async\lib\async.js:246:17)
at C:\Users\tjesu\Desktop\pidal\pidal-v4\node_modules\async\lib\async.js:44:16
at C:\Users\tjesu\Desktop\pidal\pidal-v4\node_modules\async\lib\async.js:723:17
at C:\Users\tjesu\Desktop\pidal\pidal-v4\node_modules\async\lib\async.js:167:37
at model.callbackWrapper (C:\Users\tjesu\Desktop\pidal\pidal-v4\node_modules\mongoose\lib\model.js:1843:11)
at next_ (C:\Users\tjesu\Desktop\pidal\pidal-v4\node_modules\hooks-fixed\hooks.js:89:34)
at fnWrapper (C:\Users\tjesu\Desktop\pidal\pidal-v4\node_modules\hooks-fixed\hooks.js:186:8)
at C:\Users\tjesu\Desktop\pidal\pidal-v4\node_modules\mongoose\lib\model.js:3324:16
at C:\Users\tjesu\Desktop\pidal\pidal-v4\node_modules\mongoose\lib\model.js:228:5
at C:\Users\tjesu\Desktop\pidal\pidal-v4\node_modules\mongoose\lib\model.js:135:7
at C:\Users\tjesu\Desktop\pidal\pidal-v4\node_modules\mongoose\node_modules\mongodb\lib\collection.js:504:5
我该怎么办?怎么样这个错误? 我试图改变曲目跟踪但我仍然有错误 谢谢!
答案 0 :(得分:0)
您可以验证Album.findById
是否会返回album
?
您的数据库查询似乎与任何文档都不匹配,因此album
未定义,因此您无法访问文档中的tracks属性。
答案 1 :(得分:0)
我认为在成功创建pushing tracks
之后,albums
文档中的track
出错了。
你应该推track._id
而不是track
。(因为我可以看到你试图挫败音轨,所以你必须在你的ObjectId
和reference
中使用schema
)。
当您推送整个track
document
时,album
可能无法成功保存。由于没有附加回调(album.save()
),因此它没有显示实际的error
。因此,当您使用album
null
文档,并且会提供album.findById
这就是使用populate('tracks')
时显示此错误的原因: cannot read property tracks of null.
Track.create(newSong, function(err, track){
if(err){
console.log(err);
} else {
track.albumId.id = req.params.album_id;
track.save();
album.tracks.push(track._id);//Here _id should be pushed.
album.save();
console.log('New Track is created');
res.redirect('/albums/' + album._id);
}
});