我试图通过使用mongoose"找到"从我的MongoDB获取文件。方法。但是我无法获得记录。它没有返回任何错误。这是我的代码
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 :(得分: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);
});
答案 1 :(得分:0)
也许这个查找从未被调用过...在查找之前做一些console.log以确保。
提供“getAlert”代码,我们无法看到Manager.js的使用位置和方式。