在Express中没有获得MongoDB的预期结果

时间:2013-10-27 16:15:26

标签: javascript node.js mongodb express mongoose

我正在运行NodeJS / w ExpressJS和MongoDB。我正在尝试创建一个非常简单的API,但有些东西无法正常工作。当我运行api.js并转到/api/media/movies时,它会给我输出结果,而不是数据库结果。我知道数据库中有结果,谁能告诉我我做错了什么?

{
  options: { },
  _conditions: {
    type: "Movie"
  },
  _updateArg: { },
  op: "find"
}

api.js

var app, express, models;

express = require('express');

models = require('./Librarian/models');

app = express();

app.get('/api/media/movies', function(req, res) {
  var movies;
  movies = models.Media.find({
    type: "Movie"
  });
  res.json(movies);
});

app.listen(3000);

./库/ models.js

var Media, config, mongoose;

mongoose = require('mongoose');

config = require('../config');

mongoose.connect(config.mongo.connectString);

Media = mongoose.model('Media', {
  type: {
    type: String
  },
  path: {
    type: String,
    unique: true
  }
});

module.exports = {
  Media: Media
};

1 个答案:

答案 0 :(得分:0)

忘了我在异步环境中工作。这有效:

var app, express, models;

express = require('express');

models = require('./Librarian/models');

app = express();

app.get('/api/media/movies', function(req, res) {
  models.Media.find({
    type: "Movie"
  }, function(err, movies) {
    res.json(movies);
  });
});

app.listen(3000);