【问题标题】:Node.Js mongoose find not workingNode.Js mongoose 发现不工作
【发布时间】:2016-08-22 20:52:37
【问题描述】:

我正在尝试使用猫鼬“查找”方法从我的 MongoDB 中获取文档。但我无法获得记录。它没有返回任何错误。这是我的代码

服务器.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);
});

模型.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'});
      }
  });
// });
}

我可以进入 Manager js。但是查找查询不起作用

请帮我解决这个问题。

谢谢。

【问题讨论】:

  • 试试这个:var alertModel = mongoose.model('alerts'); alertModel.find({}, function (err, docs) { if(!err) console.log(docs); });
  • @Md.NazmulHossainBilash - 试过这个。没有运气
  • 您的要求路径有效吗?
  • @shaishabroy - 是的。有效
  • 您的回调是否被实际调用,即如果您将console.log('test') 放在您的回调中,您是否会在控制台中看到“测试”?

标签: node.js mongodb mongoose


【解决方案1】:

试试这些代码。我正在使用这些代码,它对我有用。

型号:

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);
});

【讨论】:

  • 输出是什么?
  • 它没有返回任何东西。
  • 您确定在您的数据库中有值吗?检查数据库并请通知我。你可以使用 robomongo
  • 使用任何数据库可视化工具检查数据库中的记录?
  • 其实我的回调函数没有触发。
【解决方案2】:

也许 find 从未被调用...在 find 之前执行一些 console.log 以确保。

提供“getAlert”的代码,我们看不到Manager.js在哪里以及如何使用。

【讨论】:

    猜你喜欢
    • 2015-01-06
    • 2023-03-04
    • 2015-10-15
    • 2017-09-10
    • 2012-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-15
    相关资源
    最近更新 更多