1

I use mithriljs request like this :

    submit = e => {
        e.preventDefault();
        const { name } = e.target;
        console.log({ name: name.value });
        m.request({
            method: "POST",
            url: "/api/v1/vendor/type",
            body: JSON.stringify({ name: name.value }),
            headers: {
                "Content-Type": "application/json",
                Accept: "application/json"
            }
        }).then(function(result) {
            console.log(result);
            const { isSuccess, msg } = result;
            if (isSuccess) {
                alert(`Tipe Vendor ${name} berhasil di tambahkan.`);
                m.route.set("/master/vendor/tipe");
            } else {
                alert(`Gagal : ${msg}`);
            }
        });
    };

and the server :

const jsonParser = bodyParser.json()
app.route("/api/v1/vendor/type")
    .post(jsonParser,(req, res) => {
        console.log(req.body);
        const { name } = req.body;
        if (!name) res.send({ isSuccess: false, msg : 'Name is Required' });
        else {
            name = name.trim();
            VendorType.create({ name }).then(result => {
                console.log(
                    `New Vendor Type created ${name} for : ${result.id}`
                );
                res.json({ isSuccess: true, data: { id: result.id } });
            });
        }
    });

but console.log(req.body); return {} when I use postman, it works. May be I wrong in m.request please help me to solve this.

yozawiratama
  • 4,209
  • 12
  • 58
  • 106
  • It might take a few questions and answers to work out what's going wrong here. Try asking on the [Mithril chatroom](https://gitter.im/mithriljs/mithril.js)! – Barney Jul 17 '19 at 08:40

1 Answers1

1

I think problem is on:

body: JSON.stringify({ name: name.value })

You should pass an object here not a string.

Or use

data: {name: name.value}

Abdelillah Aissani
  • 3,058
  • 2
  • 10
  • 25