Why I can't retrieve any result when I use mongoose in my app
mongoose.connect('mongodb://localhost:27017/codealong');
This is how I connect to MongoDB and when I start to post any data is success but no result and in my browser
just get the result like that [ ] empty square brackets
This is my dependency
"dependencies": {
"body-parser": "^1.18.1",
"express": "^4.15.4",
"mongoose": "^4.11.12",
"nodemon": "^1.12.1"
}
here is my server.js file
var express = require('express');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var Vechile = require('./app/models/vich');
var router = express.Router();
var app = express();
app.use(bodyParser.urlencoded({ extended: true }));
var port = process.env.PORT || 3000
mongoose.connection.openUri('mongodb://localhost:27017/codealong')
.once('open', () => console.log('Good to go !'))
.on('error', (error) => {
console.warn('Warning', error);
});
app.use('/api', router);
router.use(function(req, res, next) {
console.log('There is something happens in the Background');
next();
});
//test router
router.get('/', function(req, res) {
res.json({ messege: 'Welcome to our Api' });
});
router.route('/vechicles')
.post(function(req, res) {
var vehicle = new Vechile(); //new instance from vehcile
vehicle.make = req.body.make;
vehicle.model = req.body.model;
vehicle.color = req.body.color;
vehicle.save(function(err) {
if (err) {
res.send(err);
}
res.json({ messege: 'Vehcile is READY....!' })
});
})
.get(function(req, res) {
Vechile.find(function(err, vehciles) {
if (err) {
res.send(err);
}
res.json({ vehciles });
});
});
router.route('/vehc/:id')
.get(function(req, res) {
Vechile.findById(req.params.id, function(err, vehcile) {
if (err) {
res.send(err);
}
res.json(vehcile);
});
});
router.route('/vehc/make/:make')
.get(function(req, res) {
Vechile.find({ make: req.params.make }, function(err, vehcile) {
if (err) {
res.send(err);
}
res.json(vehcile);
});
});
router.route('/vehc/color/:color')
.get(function(req, res) {
Vechile.find({ color: req.params.color }, function(err, vehcile) {
if (err) {
res.send(err);
}
res.json(vehcile);
});
});
app.listen(port, function() {
console.log('Server is running on port: ' + port);
});
and this is mongoose module
var mongoose = require('mongoose'); var Schema = mongoose.Schema;
var VichSchema = new Schema({ make: String, model: String, color: String });
module.exports = mongoose.model('Vichle', VichSchema);
链接地址: http://www.djcxy.com/p/89566.html上一篇: 错误module.exports猫鼬