我是 MEAN 堆栈的初学者,正在尝试学习以下教程:adrianmejia.com/blog/2014/10/01/creating-a-restful-api-tutorial-with-nodejs-and-mongodb/。我正在使用 Cloud 9 在线 IDE。
本教程的一部分涉及使用 curl 和 Postman 测试数据库 POST。我成功发布了一个 GET 请求,在本教程的这一点上,它给了 med 一个空数组。
我使用
curl -XGET myc9urlhere:8080/todos
- 成功(得到 [])
并尝试curl -XPOST myc9urlhere:8080/todos -d 'name=Somename&completed=false¬e=somenote'
- 失败
堆栈跟踪如下:
ValidationError: Todo validation failed
at MongooseError.ValidationError (/home/ubuntu/workspace/todo-api/node_modules/mongoose/lib/error/validation.js:23:11)
at model.Document.invalidate (/home/ubuntu/workspace/todo-api/node_modules/mongoose/lib/document.js:1486:32)
at model.Document.set (/home/ubuntu/workspace/todo-api/node_modules/mongoose/lib/document.js:753:10)
at model._handleIndex (/home/ubuntu/workspace/todo-api/node_modules/mongoose/lib/document.js:596:14)
at model.Document.set (/home/ubuntu/workspace/todo-api/node_modules/mongoose/lib/document.js:556:24)
at model.Document (/home/ubuntu/workspace/todo-api/node_modules/mongoose/lib/document.js:68:10)
at model.Model (/home/ubuntu/workspace/todo-api/node_modules/mongoose/lib/model.js:47:12)
at new model (/home/ubuntu/workspace/todo-api/node_modules/mongoose/lib/model.js:3250:13)
at /home/ubuntu/workspace/todo-api/node_modules/mongoose/lib/model.js:1864:51
at /home/ubuntu/workspace/todo-api/node_modules/mongoose/node_modules/async/internal/parallel.js:27:9
at eachOfArrayLike (/home/ubuntu/workspace/todo-api/node_modules/mongoose/node_modules/async/eachOf.js:57:9)
at exports.default (/home/ubuntu/workspace/todo-api/node_modules/mongoose/node_modules/async/eachOf.js:9:5)
at _parallel (/home/ubuntu/workspace/todo-api/node_modules/mongoose/node_modules/async/internal/parallel.js:26:5)
at parallelLimit (/home/ubuntu/workspace/todo-api/node_modules/mongoose/node_modules/async/parallel.js:85:26)
at /home/ubuntu/workspace/todo-api/node_modules/mongoose/lib/model.js:1882:5
at Function.create (/home/ubuntu/workspace/todo-api/node_modules/mongoose/lib/model.js:1852:17)
需要注意的是,我也使用 Chrome 扩展 Postman 进行了尝试。该教程说我必须使用x-www-form-urlencoded
它返回相同的错误。见截图。如果我改用表单数据设置,我实际上成功地将一些数据插入数据库,但只是虚假的东西。请参阅第二个屏幕截图。
我真的尝试过自己研究 - 也许本教程也不是最好的起点:)。下面我将包含有关代码的一些详细信息。
非常感谢您提供解决此问题的任何帮助。
与本教程相同,我为待办事项列表项创建了一个模型,如图所示 (Todo.js):
var mongoose = require('mongoose');
var TodoSchema = new mongoose.Schema({
name: String,
completed: Boolean,
note: String
});
module.exports = mongoose.model('Todo', TodoSchema);
现在在我的快速路由文件 todos.js 中,我有以下代码:
var express = require('express');
var router = express();
//express.Router() was the original here, that failed on router.get
//as router was then undefined for some reason.
var Todo = require('../models/Todo.js');
/* GET /todos listing. */
router.get('/', function(req, res, next) {
Todo.find(function (err, todos) {
if (err) return next(err);
res.json(todos);
});
});
/* POST /todos */
router.post('/', function(req, res, next) {
Todo.create(req.body, function (err, post) {
if (err) return next(err);
res.json(post);
});
});
module.exports = router;
因为我不知道这里到底出了什么问题,所以我还将发布我的 app.js 的摘录,显示我的数据库连接
var routes = require('./routes/index');
var users = require('./routes/users');
var todos = require('./routes/todos');
var app = express();
// load mongoose package
var mongoose = require('mongoose');
// Use native Node promises
mongoose.Promise = global.Promise;
// connect to MongoDB
mongoose.connect('mongodb://'+process.env.IP+'/todo-api')
.then(() => console.log('connection succesful'))
.catch((err) => console.error(err));
app.use('/', routes);
app.use('/users', users);
app.use('/todos', todos);
以及生成的 package-json(我使用的是 Cloud 9 IDE)
{
"name": "application-name",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node app.js"
},
"dependencies": {
"ejs": "*",
"express": "3.2.6",
"mongoose": "^4.7.6"
}
}