2016-08-22 86 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在何處以及如何使用。