tinyhttp/milliparsec

shows undefined in http delete request

Kamrulhasan12345 opened this issue · 7 comments

Hello,
I am recieving the following error while using json() from milliparsec:

TypeError: Cannot read property 'password' of undefined
    at file:///D:/NDESC/index.js:407:18
    at file:///D:/NDESC/node_modules/@tinyhttp/app/dist/index.js:133:19
    at file:///D:/NDESC/node_modules/@tinyhttp/app/dist/index.js:346:40
    at loop (file:///D:/NDESC/node_modules/@tinyhttp/app/dist/index.js:349:86)
    at next (file:///D:/NDESC/node_modules/@tinyhttp/app/dist/index.js:350:71)
    at compression (D:\NDESC\node_modules\compression\index.js:220:5)
    at file:///D:/NDESC/node_modules/@tinyhttp/app/dist/index.js:136:13
    at file:///D:/NDESC/node_modules/@tinyhttp/app/dist/index.js:346:40
    at loop (file:///D:/NDESC/node_modules/@tinyhttp/app/dist/index.js:349:86)
    at next (file:///D:/NDESC/node_modules/@tinyhttp/app/dist/index.js:350:71)

The code of that route is here:

app.delete("/delete/:username", async (req, res) => {
  try {
    if (req.body.password) {
      await users
        .orderByKey()
        .equalTo(req.params.username)
        .once("value", (data) => {
          if (data.exists()) {
            bcrypt
              .compare(
                req.body.password,
                data.child(`${req.params.username}/password`).val()
              )
              .then((r) => {
                if (e) {
                  handleE(e, "bcrypt error");
                  return res.sendStatus(500);
                }
                if (r) {
                  users
                    .child(req.params.username)
                    .remove()
                    .then(function () {
                      res
                        .status(200)
                        .json({ msg: "User was deleted successfully" });
                    });
                } else {
                  return res.status(401).json({ msg: "Wrong password" });
                }
              })
              .catch((e) => {
                handleE(
                  e,
                  `error while comapring passwords in ${req.originalUrl}`
                );
              });
          } else {
            res.status(404).json({ msg: "User not found" });
          }
        });
    }
  } catch (e) {
    await handleE(e, req.originalUrl);
    return res.status(500).json({ msg: "There must be some probes!" });
  }
});

The request body was like this:

{
    "username":"mohammadkh",
    "old_pass":"mypass",
    "password":"mynewpass",
    "pic_url":"kdkdj",
    "first_name":"Mohammad Kamrul",
    "last_name":"Hasan",
    "email":"kamrulhasan59246@gmail.com",
    "col_no":"4662",
    "ref_code":"6262s",
}

When I faced this error I tried with express.json() and that worked perfectly. But I am wanting to use milliparsec as it is very fast.

I also dont know what might be behind it, so....

Hope you would figure it out

Milliparsec currently only parses POST, PUT, and PATCH requests. But, knowing this, and after re-reading HTTP DELETE spec again that it might have a body, @talentlessguy what do you think?

any updates?

@aldy505 I'm confused. Does it or does not have request body?

@aldy505 I'm confused. Does it or does not have request body?

it may have a body, let's just allow it lol

@Kamrulhasan12345 see latest release

@aldy505 @talentlessguy Thank you very much for all these !