0
我得到下面的錯誤,使用快遞在Node.js中執行應用程序。 我使用的貓鼬爲我的數據庫操作下面我詳細說明了我的設計TypeError:無法讀取未定義Mongoose的屬性'推'
Party.js
var mongoose = require("mongoose");
var partySchema = new mongoose.Schema({
partyName: String,
songs: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "Song"
}
]
});
module.exports = mongoose.model("Party", partySchema);
Song.js
var mongoose = require("mongoose");
var songsSchema = new mongoose.Schema({
videoId: String,
videoName: String
});
module.exports = mongoose.model("Song", songsSchema);
我App.js
app.post("/search/addSong", function(req, res) {
Party.find({partyName:"hello"},function(err,party){
if(err){
console.log("Failed in find");
} else {
console.log(party);
// console.log(typeof(req.body.videoId));
var videoId = req.body.videoId;
var newSong = [ {
videoId:req.body.videoId,
videoName:req.body.videoName
}
];
Song.create(newSong, function(err, createdSong){
if(err){
console.log("Error creating a new party");
} else {
console.log(createdSong);
party.songs.push(createdSong);// ERROR ON THIS LINE
party.save();
res.redirect("/search");
}
});
}
});
res.render("addSong");
});
當我將歌曲添加到歌曲時,我可以單獨創建派對和歌曲的收藏對象我得到以下錯誤:
TypeError: Cannot read property 'push' of undefined
任何人都可以請讓我知道我在這裏失蹤.. !!
在此先感謝。
問題是與我試圖將對象推入另一個對象的行。 –
當您按照答案中的建議使用findOne時,問題仍然存在。 –