【问题标题】:How to send query parameters with axios to backend如何使用 axios 将查询参数发送到后端
【发布时间】:2020-06-16 23:53:28
【问题描述】:

我试图在点击提交时发送一个字符串和数字值,这些值将存储在数据库集合(日志集合)中。 我正在使用 MERN 堆栈。我尝试通过 Axios 发送它,但是当我在后端控制台记录“req.params”时,我得到了未定义。为什么我没有在后端输入“订单”的值?我确信有些事情我做的不对,但我可以弄清楚。我需要帮助。



handleSubChange(id, quantity){

// these lines of code below gets the inputted values and then subtracts from the total quantity

    if ((parseInt(quantity) - parseInt(this.state.minus))<0) {
      alert('The input value is too high.');
    }else{
      var nums = parseInt(quantity)-parseInt(this.state.minus);

// and just before I send the value of "nums" via Axios, I get the user to type in destination for the item and add the inputted value of "order" on the Axios URL

      const order = prompt("Please enter item destination")

      axios.get("/api/inventory/add_product/" + id + "/" + nums + "/" + parseInt(quantity) + "/" + order)
        .then(res => {
          console.log(res);
        })
        .catch(err => {
          console.log(err);
        })
    }

  }

这里是后端路由


router.get("/add_product/:id/:num/:quantity/:order",(req,res) => {
  var id = req.params.id;
  var quantity = req.params.quantity;

//i then console log req.params.order, i get undefined
console.log(req.params.order)

  // console.log('id----', id);

  var num_mod = req.params.num;

  var modified_count = parseInt(num_mod) - parseInt(quantity);

  console.log('num_mod----', num_mod);

  Inventory.findByIdAndUpdate(id,{quantity: parseInt(num_mod)},{new:true},function(err,inventory){
    if (err){
      console.log("err",err);
      res.status(500).send(err);
    } else {
      console.log(inventory.name);
      console.log(inventory.order)
      const newLog = new Log({
        name:inventory.name,
        description:inventory.description,
        price:parseInt(inventory.price),
        quantity:parseInt(inventory.quantity),
        modified_quantity: parseInt(modified_count),
        order:inventory.order,

      });
      newLog.save(function(err, Log) {
        if (err){
          console.log(err);
        } else{
          console.log('add log success');
          res.send(inventory);
        }
      });
    }
  });

});

库存模型

const mongoose= require('mongoose');


//create Schema
const InventorySchema = new mongoose.Schema({
   // _id: mongoose.Schema.Types.ObjectId,
   name : { type: String, required: true },
   description : { type: String, required: true },
   price : { type: Number, required: true },
   quantity : { type: Number, required: true },
   supplier : String,
   taxable : Boolean,
   order:String,


// Create model from the schema
const Inventory = mongoose.model("Inventory", InventorySchema);


// Export model
module.exports = Inventory;

【问题讨论】:

    标签: node.js reactjs express mongoose


    【解决方案1】:

    问题是您的后端路由定义,在:order 之前缺少 /。

    router.get("/add_product/:id/:num/:quantity:order",(req,res) => {
    //                                          ^ missing / 
    

    改为:

    router.get("/add_product/:id/:num/:quantity/:order",(req,res) => {
    //                                         ^ correct
    

    【讨论】:

      猜你喜欢
      • 2021-11-16
      • 2021-06-06
      • 1970-01-01
      • 2019-04-29
      • 2019-04-09
      • 1970-01-01
      • 2018-07-06
      • 2019-06-20
      • 2021-03-24
      相关资源
      最近更新 更多