似乎代码对我来说没问题,但我得到一个空数组。任何人都可以帮我这个。我看了很多教程,我无法理解我在这里做错了什么。非常感谢您的帮助
DruginformationController.js
var druginformation = require('../models/DruginformationModel');
module.exports = {
get: function (req, res) {
druginformation.find({}).exec(function (err, result) {
res.send(result);
})
}
}
DruginformationModel.js
var mongoose = require('mongoose');
module.exports = mongoose.model('druginformation',{
Category:String,
Type:String,
Name:String,
Price:Number,
Reorderlevel:Number,
Dangerlevel:Number,
Remarks:String
});
server.js
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
app.use(bodyParser.json());
app.use(cors);
var druginformation = require('./controllers/DruginformationController');
app.get('/api/getdruginformation',druginformation.get);
mongoose.Promise = global.Promise;
mongoose.connect("mongodb://root:root@xxxx.mlab.com:xxxx/xx-xx", function (err, db) {
if (!err) {
console.log("we are connected to mongo");
}
})
var server = app.listen(5000, function () {
console.log('listening on port ', server.address().port)
})
答案 0 :(得分:0)
尝试使用你的mongoose模式的方法来简单地使用“find”来获取数据,并且没有人口,就像这样:
router.get('/api/getdruginformation', function(req, res) {
druginformation.find(function(err, data){
if(err) throw err;
res.send(data);
});
});