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