2016-08-22 89 views
3

我想通过使用猫鼬“find”方法从我的MongoDB中获取文档。但我无法获得记录。它没有返回任何错误。这里是我的代码Node.Js猫鼬发现不工作

Server.js

var express  = require('express'); 
var bodyparser = require('body-parser'); 
var mongoose = require('mongoose'); 
var cors  = require('cors'); 

var app   = express(); 

var http  = require('http').Server(app); 
var io   = require('socket.io')(http); 

mongoose.promise = global.promise; 
mongoose.connect("mongodb://localhost:27017/testdb"); 
app.use(bodyparser.json()); 
app.use(cors()); 
app.use(express.static(__dirname + 'server')); 

var api  = require('./routes/apihandler')(app, express, io); 

app.use('/alerts', api); 

http.listen(3000, function(err){ 
    if(err) { 
    console.log(err); 
    } else { 
    console.log('Listening PORT' + config.port); 
    } 
}); 

apihandler.js

var request  = require('request'); 
var express  = require('express'); 
var alertRoutes = require('../../services/routes/alertRoutes'); 
var api   = express.Router(); 

module.exports = function(app, express, io){ 
    api.get('/getAllAlerts/:retailerId/:loyaltyId', getAllAlerts); 
    return api; 
} 

function getAllAlerts(req, res, callback) { 
    console.log('apihandler'); 

    try { 

     alertRoutes.getAllAlerts(req,res,callback); 



    } 
    catch(err) { 
     res.send({"status" : "error", message: err.mes}); 
    } 

} 

Manager.js

var alertModel   = require('./../models/alertModel'); 
alertModel.find({}, function (err, docs) { 
    console.log(err); 
    console.log(docs); 
}); 

Model.js

var mongoose = require('mongoose'); 

var alertSchema = new mongoose.Schema({ 
    id:{ type: String }, 
    alertType:{ type: Number}, 
    retailerId: { type: String }, 
    loyaltyId: { type: String }, 
    email: { type: String }, 
    description: { type: String},  
    locationId:{type: String }, 
    reason:{type: String}, 
    reasonDescription:{type: String}, 
    capturedReasonsList:mongoose.Schema.Types.Mixed 
},{ collection: 'alerts' }); 

module.exports = mongoose.model('alerts', alertSchema); 

alertRoutes.js

//Get all Alerts 
function getAllAlerts(req, res, callback){ 
    console.log('getAllAlerts'); 
= 
    var srchData = { 
     retailerId:req.params.retailerId, 
     loyaltyId:req.params.loyaltyId 
    }; 
retailerId='+req.params.retailerId+'&loyaltyId='+req.params.loyaltyId; 
    alertManager.getAlert(srchData,function(err,alertDetails){ 
    console.log('alertDetails',alertDetails); 
     if (err) throw err; 
     if(alertDetails && alertDetails.length > 0){ 
      res.status(200).send(alertDetails); 
     }else{ 
      res.status(200).send({success: false,message: 'Alert Details not Found'}); 
     } 
    }); 
// }); 
} 

我能抽到经理JS。但查找查询不起作用

请帮我解决这个问题。

谢谢。

+0

试试这个: var alertModel = mongoose.model('alerts'); (!err) console.log(docs); }); –

+0

@ Md.NazmulHossainBilash - 试过了。没有运气 – Jegan

+0

是你需要的路径有效吗? –

回答

0

试试这些代码。我正在使用这些代码&它适用于我。

型号:

var mongoose = require('mongoose') 
    , Schema = mongoose.Schema 

    var alertSchema = mongoose.Schema({ 

    id:{ type: String }, 
    alertType:{ type: Number}, 
    retailerId: { type: String }, 
    loyaltyId: { type: String }, 
    email: { type: String }, 
    description: { type: String},  
    locationId:{type: String }, 
    reason:{type: String}, 
    reasonDescription:{type: String}, 
    capturedReasonsList:mongoose.Schema.Types.Mixed 

    }); 
    mongoose.model('Alert',alertSchema); 

服务器代码:

var mongoose = require('mongoose'); 
var alertModel = mongoose.model('Alert'); 
alertModel.find().exec(function (err, docs) { 
    if(!err) 
    console.log(docs); 
}); 
+0

的同名文件。不工作。 – Jegan

+0

什么是输出? –

+0

它没有返回任何东西。 – Jegan

0

也许找到的是从来没有叫......做一些的console.log查找之前,为了确保。

提供“getAlert”代码,我们无法看到Manager.js在何处以及如何使用。