I am using axios for post request which is giving me correct response data but with an additional field
[[Prototype]]: Object.
I don’t want this in my apis. By testing with postman, this additional field is not showing. Why Axios is giving me this additional field and how I can get rid of it?
Here is my react code.
const submitHandler = (event) => {
event.preventDefault();
let name = event.target.name.value;
let head = parseInt(event.target.head.value);
console.log(name, head);
const body = {
name: name,
flag: 1,
accountNumber: 100,
headNo: head,
sumDebit: 0,
sumCredit: 0,
};
axios
.post("http://localhost:5000/api/accounts/create", body)
.then((res)=>{console.log (res.data);})
.catch((err) => console.log(err));
};
Here is my express code. I am only using these middlewares.
app.use(cors({origin:true,credentials:false}));
app.use(express.json({ extended: true }));
app.use("/api/accounts", account_route);
app.use("/api/transaction", transaction_route);
app.use("/api/account-info", accounts_route);
My Api code is sent in this format.
res.json(reqData);
2
Answers
That property is the standar property of every javascript object, that property is not returned by your API, javascript engine add this property in the browser to work with that object
See: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Object_prototypes
You can simply ignore it and use the data relevant to your app.
What you see there is a javascript object property added to your object. As part of prototype inheritance. So the browser can work with it.