所以我正在审查并练习使用节点mongoose和express来制作rest api。我在制作api的帖子部分时遇到问题,因为我收到一个错误,我不确定如何修复。我有点确定怎么做put和delete部分。但如果有人能够展示出什么样子。会很棒。
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)
我一直在努力解决它,但不确定如何。
答案 0 :(得分:0)
<强> req.body 强>
包含请求正文中提交的键值对数据。通过 默认情况下,它是未定义的,并在使用正文解析时填充 中间件,如body-parser和multer。
以下示例显示了如何使用正文解析中间件 填充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);
});
答案 1 :(得分:0)
//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'});
});
})
答案 2 :(得分:0)
要提交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());
})
});