Mongodb插入数据API没有响应
问题描述:
我需要一个API从Android插入数据到mongodb,我遵循this,代码可以运行,没有错误,并且我使用POSTMAN尝试发布一些数据,它总是说无法获取任何答复,似乎有错误连接到192.168.1.105:3000/songs ,但我可以使用查找所有api和findByID。 这花了我数小时的时间,请大家帮忙,所以压低:(Mongodb插入数据API没有响应
app.js
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
songs = require('./routes/route');
var jsonParser = bodyParser.json();
app.post('/songs', jsonParser, function (req, res) {
if (!req.body) return res.sendStatus(400);
app.get('/', function (req, res) {
res.send('Hello World!');
});
app.listen(3000, function() {
console.log('Example app listening on port 3000!');
});
app.get('/songs',songs.findAll);
app.get('/findById/:id',songs.findById);
app.get('/songs',songs.addSong);
路线
var mongoose = require('mongoose');
var mongo = require('mongodb');
var uri = "mongodb://XXXX:[email protected]:61365/aweitest";
mongoose.connect(uri);
// we're connected!
var db = mongoose.connection.db;
var BSON = require('bson').BSONPure;
var body = require('body-parser');
db.on('error', console.error.bind(console, 'connection errrrrrrrror:'));
db.once('open', function() {
console.log("mongodb is connected!!");
});
exports.findAll = function(req, res) {
db.collection('songs',function(err, collection) {
collection.find().toArray(function(err, items) {
res.send(items);
});
});
};
exports.findById = function(req, res) {
var id = req.params.id;
console.log('Retrieving song: ' + id);
db.collection('songs', function(err, collection)
collection.findOne({'_id':new BSON.ObjectID(id)}, function(err, item) {
res.send(item);
});
});
};
exports.addSong = function(req, res) {
var song = req.query;
console.log('Adding song: ' + JSON.stringify(song));
db.collection('songs', function(err, collection) {
collection.insert(song, {safe:true}, function(err, result) {
{console.log('Success: ' + JSON.stringify(result[0]));
res.send(result[0]);
}
});
});
};
答
好了,所以我在你的代码改变了一些东西。我成功获取app.js文件中的请求主体请看代码
route.js
var mongoose = require('mongoose');
var mongo = require('mongodb');
var uri = "mongodb://localhost:27017/test";
mongoose.connect(uri);
// we're connected!
var db = mongoose.connection.db;
var BSON = require('bson').BSONPure;
var body = require('body-parser');
db.on('error', console.error.bind(console, 'connection errrrrrrrror:'));
//db = mongoose.connection.db;
db.once('open', function() {
console.log("mongodb is connected!!");
});
exports.addSong = function(req, res) {
var song = req.body;
console.log('Adding song: ' + JSON.stringify(song));
db.collection('songs', function(err, collection) {
collection.insert(song, {safe:true}, function(err, result) {
if (err) {
res.send({'error':'An error has occurred'});
} else {
console.log('Success: ' + JSON.stringify(result[0]));
res.send(result[0]);
}
});
});
}
请注意,我已将URI更改为localhost,因此请将其更改为您指定的URI。
app.js
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
songs = require('./routes/route');
app.post('/songs', function(req, res) {
console.log("Request.Body : " + JSON.stringify(req.body));
if (!req.body) return res.sendStatus(400);
songs.addSong(req, res);
});
app.get('/', function (req, res) {
res.send('Hello World!');
});
app.listen(3000, function() {
console.log('Example app listening on port 3000!');
});
app.get('/songs',songs.addSong);
由于您唯一的问题是,你是不是能够得到请求主体。您将可以通过此代码获取它。
此外,由于你的app.get('/songs',songs.addSong);
是类型的GET,你就无法通过它来发送POST 数据。因此,将其更改为
app.post('/songs',songs.addSong);
希望这有助于。
+0
它工作!哦,我的上帝,你让我的一天! –
你能修正你的代码中的缩进吗?这真的很难阅读。 – JohnnyHK
对不起,1分钟;) –
@JohnnyHK好吧,我尽力而为;) –