Home > OS >  Node JS(Express js)- I'm able to log the error with console.log but in postman I cannot read th
Node JS(Express js)- I'm able to log the error with console.log but in postman I cannot read th

Time:12-07

I am doing custom post API, I can able to log and read the error but when I tried to send it through res.send(err) and when I checked it in postman, I only see an empty object in errorMessage in variable out. Can anyone please help me out.

Here is my code

const insertItemDetails = async (body, res) => {
  const table = new sql.Table("table_name");
  table.create = false;
  table.columns.add("jsuordref", sql.VarChar(50), {
    nullable: false,
    primary: true,
  });
  table.columns.add("itemno", sql.Int, {
    nullable: false,
    primary: true,
  });
  table.columns.add("apispec", sql.VarChar(250), { nullable: false });
  table.columns.add("customerspec", sql.VarChar(250), { nullable: false });
  table.columns.add("grade", sql.VarChar(50), { nullable: false });
  let dDate = Date();
  console.log("this is --------------_______>>>"   dDate);
  let smalldate = moment(dDate).format("YYYY-MM-DD");
  let tmstpDate = moment(dDate).format("YYYY-MM-DD HH:mm:ss:SSSS");

  for (let i = 0; i < body.itemDetails.length; i  ) {
    table.rows.add(
      body.itemDetails[i].jsuNumber,
      body.itemDetails[i].itemNo,
      body.itemDetails[i].apiSpec,
      body.itemDetails[i].customerSpec,
      body.itemDetails[i].grade
    );
  }
  const request = new sql.Request();
  request.bulk(table, (err, rows) => {
    if (!err) {
      var out = {
        code: 200,
        status: "OK",
        message: "JSUOrder successfully created!!",
        jsuOrder: body.jsuNumber,
        response: rows,
      };
    } else {
      var out = {
        code: 400,
        message:
          "Something went wrong while uploading Item Details, Please contact IT",
        errorMessage: { err },
      };
    }
    console.log(err);
    res.send(out);
  });
};

CodePudding user response:

It could be because you are sending back the error object and not the message.

Try changing

errorMessage: { err }

To

errorMessage: err.message

CodePudding user response:

You are sending

errorMessage: { err },

you should be sending

errorMessage: err,

for the error object to be displayed.

remove "{ }" around err object

  • Related