在 post、put 和 delete 上使用 mongoose 和 node.js 休息 api
Posted
技术标签:
【中文标题】在 post、put 和 delete 上使用 mongoose 和 node.js 休息 api【英文标题】:restapi using mongoose and node.js on post,put, and delete 【发布时间】:2017-01-10 04:45:59 【问题描述】:所以我正在审查和练习使用 node mongoose 和 express 制作 rest api。我在将帖子作为 api 的一部分时遇到问题,因为我遇到了一个我不确定如何修复的错误。我有点确定如何进行放置和删除部分。但是,如果有人可以展示那会是什么样子。会很棒的。
mongoose.connect('mongodb://localhost:27017/pokemon');
var pokemonSchema =
pokeid: Number,
name:String,
type:String,
weight:String,
height: String,
date: type: Date, default: Date.now ,
summary: String
var Pokemon = mongoose.model('Pokemon', pokemonSchema);
var app = express();
app.use(cors());
app.get('/api', function(req, res)
res.json( message: 'hooray! welcome to our api!' );
);
//gets the all the pokemon
app.get('/pokemon', function(req, res)
Pokemon.find(function(err, doc)
res.send(doc);
);
);
//gets a specific pokemon
app.get('/pokemon/:id', function(req, res)
Pokemon.findOne( _id: req.params.id,function (err, doc)
res.send(doc);
)
)
//create a new pokemon
app.post('/pokemon', function(req, res)
var pokemon = new Pokemon();
pokemon.pokeid = req.body.pokeid;
pokemon.name = req.body.name;
pokemon.type = req.body.type;
pokemon.weight = req.body.weight;
pokemon.height = req.body.height;
pokemon.summary = req.body.summary;
//save pokemon
pokemon.save(function(err)
if (err)
res.send(err);
res.json( message: 'pokemon created');
);
)
//update the pokemon
app.put('/pokemon/:id', function(req, res)
)
app.delete('/pokemon/:id', function(req, res)
);
错误
TypeError: Cannot read property 'pokeid' of undefined
<br> at /home/g62/pokemonsite/server/server.js:54:30
<br> at Layer.handle [as handle_request] (/home/g62/pokemonsite/server/node_modules/express/lib/router/layer.js:95:5)
<br> at next (/home/g62/pokemonsite/server/node_modules/express/lib/router/route.js:131:13)
<br> at Route.dispatch (/home/g62/pokemonsite/server/node_modules/express/lib/router/route.js:112:3)
<br> at Layer.handle [as handle_request] (/home/g62/pokemonsite/server/node_modules/express/lib/router/layer.js:95:5)
<br> at /home/g62/pokemonsite/server/node_modules/express/lib/router/index.js:277:22
<br> at Function.process_params (/home/g62/pokemonsite/server/node_modules/express/lib/router/index.js:330:12)
<br> at next (/home/g62/pokemonsite/server/node_modules/express/lib/router/index.js:271:10)
<br> at cors (/home/g62/pokemonsite/server/node_modules/cors/lib/index.js:179:7)
<br> at /home/g62/pokemonsite/server/node_modules/cors/lib/index.js:229:17
<br> at originCallback (/home/g62/pokemonsite/server/node_modules/cors/lib/index.js:218:15)
<br> at /home/g62/pokemonsite/server/node_modules/cors/lib/index.js:223:13
<br> at optionsCallback (/home/g62/pokemonsite/server/node_modules/cors/lib/index.js:204:9)
<br> at corsMiddleware (/home/g62/pokemonsite/server/node_modules/cors/lib/index.js:209:7)
<br> at Layer.handle [as handle_request] (/home/g62/pokemonsite/server/node_modules/express/lib/router/layer.js:95:5)
<br> at trim_prefix (/home/g62/pokemonsite/server/node_modules/express/lib/router/index.js:312:13)
我一直在尝试修复它,但不确定如何。
为此使用邮递员
【问题讨论】:
你在req.body
POST
通话中得到什么?
我正在关注本教程scotch.io/tutorials/…。 req.body 应该与架构匹配。
添加控制台并检查req.body
的值。还向我们展示您如何向/pokemon
提出请求?
使用邮递员检查api和东西
只要把这一行 console.log(req.body);以上 var pokemon = new Pokemon();如果你打电话给route
,那会打印出来
【参考方案1】:
req.body
包含在请求正文中提交的数据键值对。经过 默认情况下,它是未定义的,并且在您使用 body-parsing 时填充 中间件,例如 body-parser 和 multer。
下面的例子展示了如何使用 body-parsing 中间件 填充 req.body。
var app = require('express')();
var bodyParser = require('body-parser');
var multer = require('multer'); // v1.0.5
var upload = multer(); // for parsing multipart/form-data
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded( extended: true )); // for parsing application/x-www-form-urlencoded
app.post('/profile', upload.array(), function (req, res, next)
console.log(req.body);
res.json(req.body);
);
请阅读链接:http://expressjs.com/en/api.html#req.body
【讨论】:
【参考方案2】://create a new pokemon
app.post('/pokemon', function(req, res)
var pokemon = new Pokemon(
pokeid = req.body.pokeid;
name = req.body.name;
type = req.body.type;
weight = req.body.weight;
height = req.body.height;
summary = req.body.summary;
);
//save pokemon
pokemon.save(function(err)
if (err)
res.send(err);
res.json( message: 'pokemon created');
);
)
【讨论】:
【参考方案3】:要提交 post/put/delete 请求,最好使用 body-parser 中间件。
例如从数据库中删除任何数据(例如口袋妖怪),您可以按照此方法。
包括/需要必要的库/模块。
var express = require('express');
var bodyParser = require('body-parser');
var router = express.Router();
var urlencode = bodyParser.urlencoded(extended: false);
现在在 API 上创建删除请求...
router.delete('/pokemon/:id', urlencode, function(req, res, next)
new Videos(id: req.body.id).destroy().then(function(result)
res.json(result.toJSON());
)
);
【讨论】:
以上是关于在 post、put 和 delete 上使用 mongoose 和 node.js 休息 api的主要内容,如果未能解决你的问题,请参考以下文章
在 AWS CloudFront 上启用 POST/PUT/DELETE?
CORS 正在阻止 PUT、DELETE 和 POST 请求,但 GET 正在工作
AJAX POST、PUT 和 DELETE 工作正常,但 GET 被 CORS 阻止
尽管有 AllowAny 权限,django-rest-framework 在 POST、PUT、DELETE 上返回 403 响应