Home > Blockchain >  POST method an array in nodejs
POST method an array in nodejs

Time:09-22

im having trouble in creating an array in postman.

What I want is to post this in the request.body(postman/insomnia)..

{
    "nama": "tests",
    "afco_id": 2,
    "business_area_id": 10,
    "approval_level": [
        {
            "sequence_level": 1,
            "user_id": 1
        },
        {
            "sequence_level": 2,
            "user_id": 2
        },
        {
            "sequence_level": 3,
            "user_id": 3
        }
    ]
}

and this is my controller code:

const postApprovalMaster = async (req, res) => {
    return (
      db.Bb_t2_Approval.create(
        {
          nama: req.body.nama,
          afco_id: req.body.afco_id,
          business_area_id: req.body.business_area_id,
          approval_level: [req.body]
        },
        {
          include: [
            {
              model: db.Bb_t3_Approval_Level,
              as: "approval_level",
            },
            {
              model: db.Bb_t1_Organisasi,
              as: "afco",
            },
            {
              model: db.Bb_t1_Organisasi,
              as: "business_area",
            },
          ],
        }
      ).then((result) =>
          res.status(201).json({
            data: result,
            status: true,
            message: "Created new approval master success",
          })
        )
        .catch((err) => {
          res.status(400).json({
            status: false,
            message: err.message,
          })
        })
    )
  }

whenever i hardcoded the req.body into the controllers approval_level, it works just fine. but i dont want it like that can anyone help? thanks a lot I appreciate it

CodePudding user response:

You can either try this

Solution 1

or if this doesnt work with node.js you can also try to just write the array attribute like this.

Solution 2

One of those should work.

CodePudding user response:

I can't comment and I'd love to help but can you try to give more examples or explain a little more @Hafizh Farhandika

  • Related