调用PUT从EJS进行表达

调用PUT从EJS进行表达

本文介绍了调用PUT从EJS进行表达的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从EJS表单中调用用expressjs创建的一个宁静的api.

I am trying to call a restful api created in expressjs from EJS form.

我尝试查看google和stackoverflow,并尝试了许多不同的方法.

I tried looking on google and stackoverflow, and trying lots of different methods.

orders.ejs:

orders.ejs:

 <ul class="orders">
<% for(var i=0; i<orders.length; i++) {%>
  <ul class="order">
    <p> coffee_id <span><%= orders[i].name %></span>
        amount: <span><%= orders[i].amount %></span>
        quantity <span><%= orders[i].quantity %></span>  </p>
  </ul>
<% } %>
</ul>



<div>
    <form action="/orders" method="POST" action="orders/?_method=PUT">
      <input type="text" placeholder="username" name="username">
      <input type="text" placeholder="coffee name" name="name">
      <input type="number" placeholder="amount" name="amount">
      <input type="number" placeholder="quantity" name="quantity">
      <button type="submit" id="order" >Submit</button>
    </form>
  </div>

nodejs.app.js:

nodejs.app.js:

app.post('/orders', (req, res) => {  // falta o EJS
  db.collection('orders').save(req.body, (err, result) => {
    if (err) return console.log(err)
      console.log(req.body);
    res.render('orders.ejs', {
      put: true
  });
  })
})

app.put('/orders/:coffeeid/:quantity', function(req, res) { ...
}

此代码可以正常工作,可以使用正确的参数分别从Postman调用它们,但是在提交EJS表单时,我需要同时调用它们.

This code works fine individually calling them from Postman with the right parameters, but I need to call them both when I submit the EJS form.

app.get('/orders/:coffeeid',function(req, res) {
  db.collection('orders').find({_id:req.params.coffeeid}).toArray, function(error,result){
      if(!error && result && quantity){
            return res.render('orders.ejs',{orders: result, coffeeid:result.coffeeid, quantity:'7'});  // <-- pass coffeeid to view
      }
  }
});

我还需要从表格中读取数量并检查PUT中的库存,因此现在我将其固定为"7"个单位,但现在无法获取/订购

I need to read quantity also from the form and check for stock in the PUT, so for now I fixed it at '7'units, but now Cannot GET /orders

推荐答案

获取订单的路线:

  app.get('/orders',function(req, res) {
       // fetch all orders and display it to view
  });

通过coffeeid显示订单显示编辑页面的路线:

route for display edit page of order by coffeeid :

app.get('/orders/:coffeeid',function(req, res) {
   var coffeeid= req.params.coffeeid;

   db.collection('orders').find({_id:coffeeid}).toArray,function(error,result){
     if(!error && result){
        return res.render('orders.ejs',{orders:result,coffeeid:result.coffeeid, quantity:'7'});
      }
    });

});

form:

 <form action="/orders/<%=coffeeid%>" method="POST">

      <input name="_method" type="hidden" value="PUT">
      <input type="text" placeholder="username" name="username">
      <input type="text" placeholder="coffee name" name="name">
      <input type="number" placeholder="amount" name="amount" value="">
      <input type="number" placeholder="quantity" name="quantity" value="<%=quantity%>">

      <button type="submit" id="order" >Submit</button>

   </form>

更新信息路线将为:

app.put('/orders/:coffeeid', function(req, res) {
   var coffeeid= req.params.coffeeid;
   var quantity= req.body.quantity;

  // save to database
}

这篇关于调用PUT从EJS进行表达的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 02:03