![](/img/trans.png)
[英]State is not being updated. When I log the data it shows “value below evaluated just now”
[英]sending requests to my own API to make changes to DB which is not being updated. I would like the DB to be updated
我无法理解如何向使用express设置的API请求CRUD方法。 在下面的代码中.get(/bears)
呈现一个表单并在app.post('/bears')
我正在使用nPmodule'请求'通过执行此操作将请求发送到api / bears路由器我希望名称文本框的attr是db上要添加到db的相同字段( bear.save(function(err){
)这不是问题所在。
有人请告诉我如何从路径(页面)到api进行调用,以便api可以从视图中的html中的表单更新数据库。
我认为Angular有这方面的服务,但我想看看如何用express表达。 在没有npm请求的情况下可以做到吗?
js摆弄了更多的代码
app.get('/bears', function(req, res){
res.render('index', {message : "You are rendered!"});
})
app.post('/bears', function(req, res){
// request.post('http://localhost:8080/api/bears',function(error, response, body){
// if(!error && response.statusCode == 200){
// var info = JSON.parse(body)
// res.send(info);
// }else{
// res.send(error);
// }
// })
request.post('http://localhost:8080/api/bears', function(error, response, body){
console.log(body);
console.log(response)
})
})
router.route('/bears')
.post(function(req, res){
var bear = new Bear();
bear.name = req.body.name;
bear.save(function(err){
if(err)
res.send(err);
res.json({message : 'Bear created!'});
})
})
.get(function(req, res){
Bear.find(function(err, bears){
if(err)
res.send(err);
res.json(bears);
console.log(bears[0].id)
})
})
这似乎有效。 我不知道我可以将req.body.name
作为'值'
app.post('/bears', function(req, res){
// request.post('http://localhost:8080/api/bears',function(error, response, body){
// if(!error && response.statusCode == 200){
// var info = JSON.parse(body)
// res.send(info);
// }else{
// res.send(error);
// }
// })
request.post({url : 'http://localhost:8080/api/bears', form : {name : req.body.name }}, function(err, httpResponse, body){
res.redirect("/alpha");
})
})
app.get('/alpha', function(req, res){
res.send('It was updated');
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.