我有一個mongodb和node.js之間的連接,但是當我測試這種方法時,總是給我null或文件,沒有在數據庫中。我究竟做錯了什麼?Web服務RESTful與node.js
index.js(主文件),在那裏我稱之爲路由器
var express = require("express"),
app = express(),
bodyPaser = require("body-parser"),
methodOverride = require("method-override"),
mongoose = require('mongoose');
var port = 3000;
mongoose.connect('mongodb://localhost:27017/task',
function(err,res){
if(err){return console.log(`Error al Conectar a la BD stablish ${err}`)};
console.log('Conexión a la BD stablish');},{ useMongoClient: true });
app.use(bodyPaser.urlencoded({ extended : false}));
app.use(bodyPaser.json({ type: 'application/json' }));
app.use(methodOverride());
var models = require('./models/task')(app, mongoose);
var TaskListCtrl = require('./controller/taskListController');
var router = express.Router();
router.get('/', function(req, res) {
res.send("Hello world!");
});
app.use(router);
var task = express.Router();
task.route('/task')
.get(TaskListCtrl.findAllTasks)
.post(TaskListCtrl.addTask);
task.route('/task/:id')
.get(TaskListCtrl.findById)
.put(TaskListCtrl.updateTask)
.delete(TaskListCtrl.deleteTask);
app.use('/api', task);
app.listen(3000, function() {
console.log("Node server running on http://localhost:3000");
});
的package.json
{
"name": "restnodetask",
"version": "1.0.0",
"description": "Proyecto de API RESTful con Node.js y Express",
"main": "index.js",
"scripts": {
"start": "nodemon index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Jean Carlo Flores",
"license": "MIT",
"dependencies": {
"body-parser": "^1.17.2",
"express": "^4.15.3",
"method-override": "^2.3.9",
"mongodb": "^2.2.30",
"mongoose": "^4.10.8"
},
"devDependencies": {
"nodemon": "^1.11.0"
}
}
控制器/ taskLisController(其中我要求的響應)
var mongoose = require('mongoose');
var Task = mongoose.model('Task');
exports.findAllTasks = function(req, res) {
Task.find(function(err, task) {
if (err) res.send(500,err.message);
console.log('GET /tasks');
res.status(200).jsonp(task);
res.body = task;
});
};
exports.findById = function(req, res) {
Task.findById(req.params.id, function(err,task) {
if(err) return res.send(500, err.message);
console.log('GET /task/' + req.params.id);
res.status(200).jsonp(task);
});
};
exports.addTask = function(req, res) {
console.log('POST');
console.log(req.body);
var task = new Task({
objectId : req.body.objectId,
title : req.body.title,
remember : req.body.remember,
date_time : req.body.date_time
});
task.save(function(err, task) {
if(err) return res.send(500, err.message);
res.status(200).jsonp(task);
});
};
exports.updateTask = function(req, res) {
Task.findById(req.params.id, function(err, task) {
task.objectId = req.body.objectId;
task.title = req.body.title;
task.remember = req.body.remember;
task.date_time = req.body.date_time;
task.save(function(err) {
if(err) return res.send(500, err.message);
res.status(200).jsonp(task);
});
});
};
exports.deleteTask = function(req, res) {
Task.findById(req.params.id, function(err, task) {
task.remove(function(err) {
if(err) return res.send(500, err.message);
res.status(200);
})
});
};
我在MongoDB的
exports = module.exports = function(app, mongoose) {
var taskSchema = new mongoose.Schema({
objectId : { type:String} ,
title : { type:String},
remember : { type: Boolean},
date_time : { type:Date}
});
mongoose.model('Task',taskSchema);
};
我可以看到的唯一明顯的事情是,當你執行'res.status(200).jsonp(tvshow)'時,'tvshow'是未定義的。 –
你真的需要控制縮進,並且在函數之間有一些空間,而你的代碼在語法方面可能是確定的,這很痛苦,因此可以幫助你 – cbll
我的道歉我真的是node.js的新手。 –