Home > Back-end >  express post api isnt executing when sent string is 18 characters long
express post api isnt executing when sent string is 18 characters long

Time:01-22

When i get the string from my discord bot, i make a post request to my api

axios.post(`http://localhost:8080/api/post/ban/${discord_id}`, {}, {
        headers: {
          key: key
        }
       }).then((response) => {
        console.log(response.data)
    })

But when its submitted the event isnt activated When i sent string the length of 17 or less or length of 19 or more it worked but not when string length is 18

app.post('/api/post/ban/:discord_id/', async function (req, res) {
  let id = req.params.discord_id
  let header = req.headers;
  if(isNaN(id)) return res.send({
    "error": {
      "message": "USER_ID_MUST_BE_NUMBER",
      "code": "400"
    }
  });
  if(id.length < 19 || id.length > 19) return res.send({
    "error": {
      "message": "ID_IS_NOT_VALID",
      "code": "400"
    }
  });
  if(header.key != key) return res.send({
    "error": {
      "message": "OWNER_ONLY",
      "code": "none"
    }
  });
  await banModel.findByIdAndUpdate(banID, {
    $addToSet: { "bannedUsers": `${id}`}
  });
  return res.send({
    "success": {
      "message": "ADDED_USER_TO_BANS",
      "code": "201"
    }
  });
});`

CodePudding user response:

i fixed it heres the answer:

        axios.post(`http://localhost:8080/api/post/ban/${discord_id}/`,{},{
        headers: {
          key: key
        }
       })
      .then(function (response) {
        console.log(response.data)
        if(response.data.error) {
          switch(response.data.error.code) {
            case "404":
              return interaction.reply("Channel not found!"); 
            case "422":
              return interaction.reply("Invalid parameters!"); 
            case "400":
              return interaction.reply("Invalid types of objects!"); 
            case "409":
              return interaction.reply("U already exist on our Database!"); 
            case "none":
              switch(response.data.error.message) {
                case "INVALID_VIDEO_URL":
                  return interaction.reply("Invalid video url")
                case "OWNER_ONLY":
                  return interaction.reply("Owner only API")
              }
            break;
          }
        }
        if(response.data.success) return interaction.reply("Succesfully added your video!")
      })
      .catch(function (error) {
        console.log(error);
      });
  •  Tags:  
  • Related