简体   繁体   中英

Cannot convert object to primitive value

I post data to node server:

{
  "form_1": [{
    "resultName": "",
    "level": "",
    "unitOrder": "",
    "winner": "",
    "winnerOrder": "",
    "date": "1",
    "score": ""
  }],
  "form_2": [{
    "resultName": "",
    "level": "",
    "unitOrder": "",
    "winner": "",
    "winnerOrder": "",
    "date": "1",
    "score": ""
  }],
  "form_3": [{
    "resultName": "",
    "level": "",
    "unitOrder": "",
    "winner": "",
    "winnerOrder": "",
    "date": "1",
    "score": ""
  }]
}

And I try to console.log the data in express server:

console.log(req.body)

but there comes the error:

TypeError: Cannot convert object to primitive value

I do not understand. so how can I get the data ?

console.log is for logging strings or simple values

as the docs say, you can format the output, as a json for example console.log("body: %j", req.body)

or, better yet, use console.dir to log the object as it is

Points to check.

  1. header type should be application/json .
  2. Check whether json is stringified .

angular code

 headers.append('Content-Type', 'application/json');
    this.http.post(url, JSON.stringify(body), { headers: headers }).map((data: Response) => data.json()).catch(this.handleError);
  1. Cors is enabled in server.js in nodeJs - express code

    var cors = require('cors'); app.use(cors());

4.Now req.body in api.js (or somefile.js ) should have the json data passed from angular form

Above is the one the combination which worked for me. Some are facing error by sending a json from angular and trying to parse a json object again in nodejs which cause a error.

Few get pre-flight error. Actually the browser might first send a pre-flight request with method OPTIONS to get allowed origins, headers and methods. So enabling cors helps to avoid this error.

What's causing this error-message in general is that the system tries to convert some object to a string but can not produce a string from the object it is trying to display as string.

That is often the case if a value does not inherit Object but is a more primitive type of value so it does not inherit the method toString() from Object.prototype.

For instance

Object.create(null) + '' 

causes this error because + "" basically means the same as calling the method toString() of the value to which you try to + "". But since Object.create(null) does not inherit the method toString() from Object.prototype this causes an error.

Similarly

(Object.create(null)) .toString() 

produces this same error.

If you add your own method toString() to such a value your added method must return a string or else you get the same error-message again because the system then calls your toString() -method but that does not produce a String.

So beware trying to print out in the log etc. values which are not Objects. Another case would be if you called alert() or confirm() etc. with such a value.

in general console.log(req.body) but when you are dealing with put you better ty out console.log('request body: ',req.body); do not

The actual reason behind this error

when Javascript is trying to cast an object to a string.

Javascript will try to call the toString() method of that object. most of the Javascript objects have toString() method inherited from Object.prototype . but some of them that have a null prototype , not have toString() method so the Javascript will fail to convert those objects to a string primitive. and can't convert object to primitive error will rise .

to create a null prototype Object in Javascript use this method

let travel = Object.create(null)

The solution

travel = {...travel}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM