Javascript 使用push()时出现mongoose错误
album.js:Javascript 使用push()时出现mongoose错误,javascript,mongodb,express,mongoose,nosql,Javascript,Mongodb,Express,Mongoose,Nosql,album.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'}, da
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);
app.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]
});
当我使用codealbum.songs.push(歌曲)时代码>,我得到错误信息:
无法调用未定义“”的方法“call”
请帮我解决这个问题。如果我想在一张专辑中存储许多歌曲,我该如何做呢?您混淆了模式和模式
在albums.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");
});
在albums.js中
mongoose.model('Song', SongSchema); // This statement registers the model
module.exports = SongSchema; // export the schema instead of the model
所以,解决这个问题的简单方法是在一个文件models.js
中安装所有模式。是这样吗?你能告诉我如何以你的方式导出模式吗?因为我试图在谷歌上找到答案,但我什么都没有,也许我使用了错误的关键字它正在运行。非常感谢乔。。请帮我解决这个问题
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
SongSchema = require('mongoose').model('Song'), // <<<<<<<<<< here should be a schema istead of a model
ObjectId = Schema.ObjectId;
mongoose.model('Song', SongSchema); // This statement registers the model
module.exports = SongSchema; // export the schema instead of the model
SongSchema = require('./songs');