简体   繁体   English

无法将对象转换为原始值

[英]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 数据:

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 console.log用于记录字符串或简单值

as the docs say, you can format the output, as a json for example console.log("body: %j", req.body)正如文档所说,您可以将输出格式化为 json,例如console.log("body: %j", req.body)

or, better yet, use console.dir to log the object as it is或者,更好的是,使用console.dir按原样记录对象

Points to check.要检查的要点。

  1. header type should be application/json .标头类型应该是application/json
  2. Check whether json is stringified .检查 json 是否被字符串化

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在 nodeJs 中的 server.js 中启用了Cors - 快速代码

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

4.Now req.body in api.js (or somefile.js ) should have the json data passed from angular form 4.现在api.js(或somefile.js )中的 req.body 应该有从 angular 形式传递的 json 数据

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.有些人通过从 angular 发送 json 并尝试在 nodejs 中再次解析 json 对象而面临错误,这会导致错误。

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.实际上,浏览器可能首先发送带有方法 OPTIONS 的预检请求,以获取允许的来源、标头和方法。 So enabling cors helps to avoid this error.因此启用 cors 有助于避免此错误。

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.如果一个值不继承 Object 但它是一种更原始​​的值类型,因此它不会从 Object.prototype 继承 toString() 方法,那么通常就是这种情况。

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 + "".导致此错误,因为 + "" 基本上与调用您尝试 + "" 的值的方法 toString() 的含义相同。 But since Object.create(null) does not inherit the method toString() from Object.prototype this causes an error.但是由于 Object.create(null) 没有从 Object.prototype 继承 toString() 方法,这会导致错误。

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.如果您将自己的方法 toString() 添加到这样的值,您添加的方法必须返回一个字符串,否则您将再次收到相同的错误消息,因为系统随后调用了您的 toString() 方法,但不会产生字符串。

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.另一种情况是,如果您使用这样的值调用 alert() 或 confirm() 等。

in general console.log(req.body) but when you are dealing with put you better ty out console.log('request body: ',req.body);一般情况下console.log(req.body)但是当你处理put你最好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 尝试将对象转换为字符串时。

Javascript will try to call the toString() method of that object. Javascript 将尝试调用该对象的toString()方法。 most of the Javascript objects have toString() method inherited from Object.prototype .大多数 Javascript 对象都有从Object.prototype继承的toString()方法。 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.但是其中一些具有null prototype ,没有toString()方法,因此 Javascript 将无法将这些对象转换为字符串原语。 and can't convert object to primitive error will rise .并且can't convert object to primitive error will rise

to create a null prototype Object in Javascript use this method在Javascript中创建一个空原型对象使用这个方法

let travel = Object.create(null)

The solution解决方案

travel = {...travel}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError:无法将对象转换为原始值 - TypeError: Cannot convert object to primitive value Javascript 错误:无法将 Object 转换为原始值 - Javascript Error: Cannot Convert Object to Primitive Value 无法使用导入的 json 将对象转换为原始值 - Cannot convert object to primitive value with imported json 将对象转换为URL编码的字符串时无法将对象转换为原始值 - Cannot convert object to primitive value in converting object into URL encoded string JSON字符串上的JSON解析抛出“无法将对象转换为原始值” - JSON parse on a JSON string throws “Cannot convert object to primitive value” 无法在反应应用程序中将 object 转换为原始值错误? - Cannot convert object to primitive value error in react application? 如何在 GAS 中将原始值转换为日期对象 - How to convert primitive value to Date object in GAS Ember.RSVP.hash在路由的模型挂钩中给出“无法将对象转换为原始值” - Ember.RSVP.hash gives 'cannot convert object to primitive value' in model hook of route 在ember js应用中按下浏览器后退按钮时,无法将对象转换为原始值 - Cannot convert object to primitive value, on pressing browser back button in ember js app 如何修复在使用 React JS 和 Bootstrap 折叠导航栏时无法将 object 转换为原始值 - How to fix cannot convert object to primitive value in collapsing of navigation bar using React JS and Bootstrap
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM