我已经设置了clould9帐户和mongolab帐户。我填写了一些如下所示的项目:
所以一切都很好。这也在Mongo实验室中显示:
问题出在我打电话的时候:
var db = mongoose.createConnection(MONGO.connectionString(), MONGO.options);
// When successfully connected
db.on('connected', function () {
console.log('Mongoose default connection');
console.log(mongoose.connection);
require('./SchemaMovie');
});
这就输出了我:
{ base:
{ connections: [ [Circular], [Object] ],
plugins: [],
models: {},
modelSchemas: {},
options: { pluralization: true } },
collections: {},
models: {},
config: { autoIndex: true },
replica: false,
hosts: null,
host: null,
port: null,
user: null,
pass: null,
name: null,
options: null,
otherDbs: [],
_readyState: 0,
_closeCalled: false,
_hasOpened: false,
_listening: false }
任何想法为什么我没有看到那里的集合电影(集合:{})?我跳过了什么吗?
答案 0 :(得分:1)
发表于OP的评论:
修复模型应写为:
var mongoose = require('mongoose');
var movieSchema = new mongoose.Schema({
title: String,
titleURL: String,
description: String,
folder: String,
year: Number,
duration: Number,
video720p: String,
trailer: String,
poster: String,
language: String,
dateAdded: { type: Date, default: Date.now },
genres: [String],
imdb: {
rating: Number,
src: String
}
}, { collection : 'Movie' });
movieSchema.statics.listMovies = function (type, count, limit, cb) {
return this.model('Movie').find({}, cb);
}
var Movie = mongoose.connection.model('Movie', movieSchema);
module.exports = Movie;
基石似乎就是这个:
, { collection : 'Movie' }
似乎应该在连接打开时添加模型,并且仅为该猫鼬连接设置模型。所以我需要改变:
var db = mongoose.createConnection(MONGO.connectionString(), MONGO.options);
到
mongoose.connect(MONGO.connectionString(), MONGO.options);
或在要做的模型中
var db = mongoose.createConnection(MONGO.connectionString(), MONGO.options);
....
var Movie = db.model('Movie', movieSchema);
module.exports = Movie;
所以我结束了这段代码:
mongoose.connect(MONGO.connectionString(), MONGO.options);
mongoose.connection.once('open', function callback () {
console.log('open');
var Movie = require('./SchemaMovie');
var query = Movie.find({});
query.exec(function (err, movie) {
if (err) {
console.log(err);
return null;
}
console.log(movie);
});
});
mongoose.connection.on('error', function(err) {
console.error('MongoDB error: %s', err);
});
作为连接