4
-- express_example |---- app.js |---- models |-------- songs.js |-------- albums.js |---- and another files of expressjs
songs.js:貓鼬錯誤時使用推送()
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
ObjectId = Schema.ObjectId;
var SongSchema = new Schema({
name: {type: String, default: 'songname'},
link: {type: String, default: './data/train.mp3'},
date: {type: Date, default: Date.now()},
position: {type: Number, default: 0},
weekOnChart: {type: Number, default: 0},
listend: {type: Number, default: 0}
});
module.exports = mongoose.model('Song', SongSchema);
album.js:
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
SongSchema = require('mongoose').model('Song'),
ObjectId = Schema.ObjectId;
var AlbumSchema = new Schema({
name: {type: String, default: 'songname'},
thumbnail: {type:String, default: './images/U1.jpg'},
date: {type: Date, default: Date.now()},
songs: [SongSchema]
});
app.js:
require('./models/users');
require('./models/songs');
require('./models/albums');
var User = db.model('User');
var Song = db.model('Song');
var Album = db.model('Album');
var song = new Song();
song.save(function(err){
if(err) { throw err; }
console.log("song saved");
});
var album = new Album();
album.songs.push(song);
album.save(function(err){
if(err) { throw err; }
console.log("save album");
});
當我使用代碼album.songs.push(song);
,我收到錯誤:
Cannot call method 'call' of undefined`.
請幫我解決這個問題。如果我想在專輯中存儲很多歌曲,我應該怎麼做?
所以,要解決這個問題簡單的方法就是安裝所有的模式在一個文件'models.js'。是對的嗎? 你能告訴我如何導出一個架構在你的方式,因爲我試圖找到谷歌,但我什麼都沒有,可能我使用了錯誤的關鍵字 – 2012-01-05 17:38:16
我更新了答案。 – qiao 2012-01-05 17:47:06
它正在運行。感謝'qiao'這麼多..請幫我解決[這裏](http://stackoverflow.com/questions/8747649/query-with-mongoose) – 2012-01-05 18:21:21