使用mongoose检索文档时找不到数据

时间:2016-02-23 08:21:47

标签: node.js mongodb express mongoose openshift

我有一个数据库:be_meta和一个集合:be_meta_records。我通过mongo shell

在文档下面插入了它
{ "_id" : ObjectId("90sd744628ba73a8199f9db6"), "be_id" : "74HHF411-9UI8-440C-UIOP-E907DAF9C93F", "item_title" : "Item 1", "item_description" : "item 1 description", "item_link" : "http://stackoverflow.com/" }

我试图通过mongoose访问相同的内容,使用下面的代码:

server.js

var wagner = require('wagner-core');

require('./models')(wagner);

var express = require('express');
var app = express();

app.set('port', process.env.OPENSHIFT_NODEJS_PORT || 8080);
app.set('ip', process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1');

var http = require('http');

app.use('/api/v1',require('./api')(wagner));

http.createServer(app).listen(app.get('port'), app.get('ip'), function(){
                              console.log('Express server listening on port ' + app.get('port'));
                              });

app.get('/', function (req, res) {
        res.send('APIs are ready :)');
        });

api.js

var express = require('express');
var status = require('http-status');

module.exports = function(wagner) {
    var api = express.Router();

    api.get('/be_meta_records/be_id/:be_id', wagner.invoke(function(BeMetaRecords){
                                                                   return function(req, res){
                                                                   console.log('be id ' + req.params.be_id);
                                                                   BeMetaRecords.findOne({be_id: req.params.be_id}, function(err, beMetaRecords) {
                                                                                         if(err){
                                                                                         return res.status(status.INTERNAL_SERVER_ERROR).json({error: err.toString()});
                                                                                         }
                                                                                         if(!beMetaRecords){
                                                                                         return res.status(status.NOT_FOUND).json({error: 'Not Found'});
                                                                                         }

                                                                                         res.json({be_meta_records: beMetaRecords});
                                                                                         });
                                                                   };
                                                                   }));

    return api;
};

models.js

var mongoose = require('mongoose');

module.exports = function(wagner){
    var connectionString = '127.0.0.1/be_meta';

    if(process.env.OPENSHIFT_MONGODB_DB_PASSWORD){
        connectionString = process.env.OPENSHIFT_MONGODB_DB_USERNAME + ':' +
        process.env.OPENSHIFT_MONGODB_DB_PASSWORD + '@' +
        process.env.OPENSHIFT_MONGODB_DB_HOST + ':' +
        process.env.OPENSHIFT_MONGODB_DB_PORT + '/' +
        process.env.OPENSHIFT_APP_NAME;
    }

    mongoose.connect('mongodb://' + connectionString);

    var BeMetaRecords = mongoose.model('BeMetaRecords', require('./be_meta_records'));

    wagner.factory('BeMetaRecords', function(){
                   return BeMetaRecords;
                   });

    return  {
        BeMetaRecords : BeMetaRecords
    };
};

be_meta_records.js

var mongoose = require('mongoose');

var beMetaRecordsSchema = {
    be_id : {type: String, required: true},
    item_title: {type: String, required: true},
    item_description : {type: String},
    item_link : {type: String}
};

module.exports = new mongoose.Schema(beMetaRecordsSchema);
module.exports.beMetaRecordsSchema = beMetaRecordsSchema;

当我尝试通过输入此URL访问相同的浏览器时:

http://127.0.0.1:8080/api/v1/be_meta_records/be_id/74HHF411-9UI8-440C-UIOP-E907DAF9C93F

我收到以下错误:

{"error":"Not Found"}

有什么想法吗?

更新:在本地IP工作

感谢@AlexBlex指出我正确的方向。

我通过更新models.js中的以下代码行解决了本地IP的错误:

var BeMetaRecords = mongoose.model('BeMetaRecords', require('./be_meta_records'));

为:

var BeMetaRecords = mongoose.model('be_meta_records', require('./be_meta_records'));

更新:在OpenShift中不起作用

当推送到openshift时,相同的代码不起作用,它显示相同的错误:

{"error":"Not Found"}

0 个答案:

没有答案