【问题标题】:Cloud9 and MongoLabCloud9 和 MongoLab
【发布时间】:2015-07-26 22:36:33
【问题描述】:

我已经设置了 could9 账号和 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 }

任何想法为什么我在那里看不到集合电影(集合:{})?我跳过了什么吗?

【问题讨论】:

  • 由于我无法发布答案(出于某种原因),我花时间写了一个答案,我是如何设法让它工作的:jsfiddle.net/9hkb1uu1

标签: mongodb cloud9-ide mlab


【解决方案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);
});

作为连接

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    相关资源
    最近更新 更多