简体   繁体   English

TypeError:无法将对象转换为原始值

[英]TypeError: Cannot convert object to primitive value

I have a texteditor (div) and there's a function to format the (selected)text inside it. 我有一个texteditor(div),里面有一个格式化(选定)文本的函数 When I mark a part of the text and chose to make it look like this (code snippet) I have to use   当我标记文本的一部分并选择使其看起来像this (代码段)时,我必须使用  to avoid some bugs and make it user-friendly. 避免出现一些错误并使其易于使用。 However this data is being sent to the server (nodeJS) and it causes a bug where the content splits up into an object and to avoid this issue, I wanted to replace   但是,此数据正在发送到服务器(nodeJS),并且会导致一个错误,即内容拆分为一个对象,为避免此问题,我想替换为  with a space before sending it to the server. 发送到服务器之前,请留一个空格。

What I did was the following 我所做的是以下

// replace   by " "
let content = $('.editor').html().replace(/( )/gi, " ");

// replace editor html
$('.editor').html(content);

// print results
console.log("html: ",$('.editor').html());

in console it displays what is expected (text : as <code>dasdasd</code> ): 在控制台中,它显示期望的内容(文本: as <code>dasdasd</code> ):

html:  as<span> </span><code>dasdasd</code><span> </span>

however on the serverside i got the following error: 但是在服务器端,我收到以下错误:

TypeError: Cannot convert object to primitive value

then i decided to print the variable which contains the content from editor (which seems fine?): 然后我决定打印包含来自编辑器内容的变量(看起来不错吗?):

{ posterContent: 'as<span> </span><code>dasdasd</code><span> </span>' }

Question: how can I replace &nbsp; 问题:我该如何更换&nbsp; with a space without having to convert html to (string) in order to avoid this error? 用空格而不需要将html转换为(string)以避免此错误?

I know you solved the problem, but you might be interested in reading this, because your problem arised from a misunderstood basic concept of web dev which is data encoding. 我知道您已经解决了该问题,但是您可能会对阅读本文档感兴趣,因为您的问题是由于Web开发的基本概念(数据编码)被误解了引起的。

As far as I understand, you can't pass the string &nbsp; 据我了解,您无法传递字符串&nbsp; to your back-end because it's parsed as an object, so I assume you either used GET or POST's application/x-www-form-urlencoded encoding to send request. 到后端,因为它被解析为对象,因此我假设您使用GET或POST的application/x-www-form-urlencoded编码来发送请求。 In simpler terms: 简单来说:

// this object
{
  a: 10,
  b: 20
}

// get passed to the server as this string
a=10&b=20

Which is fine. 没关系 That's one way to do. 那是一种方法。 But you have to deal with proper encoding for sending special characters, example: 但是您必须处理用于发送特殊字符的正确编码,例如:

// you have this object:
{
  a: 10,
  b: 'hello&world'
}

// you encode it naively to this
a=10&b=hello&nbsp;world

// the server understands this
{
  a: 10,
  b: 'hello',
  nbsp: ';world'
}

The & creates the bug, because it's a special character and won't be treater as part of a string. &会创建错误,因为它是一个特殊字符,不会被视为字符串的一部分。 Even if you find a trick to not use &nbsp , or to replace it by a space, you'll think you have solved the problem, but... almost all unicode characters are special characters and need to be encoded so as to not create bugs . 即使您找到了不使用&nbsp或将其替换为空格的技巧,您也认为您已经解决了问题,但是... 几乎所有的unicode字符都是特殊字符,需要进行编码以免创建虫子

Encode your strings using encodeURIComponent , or POST your data with a different encoding (for example, JSON). 使用encodeURIComponent编码字符串,或使用其他编码(例如JSON)发布数据。 I'd personally use a function like fetch which does all the job for you and spare you with all the encoding-related issues: 我个人会使用像fetch这样的函数来为您完成所有工作,并避免与编码有关的所有问题:

 let data = { userId: 1, id: 1 } fetch('https://jsonplaceholder.typicode.com/posts',{ method: 'POST', data: JSON.stringify(data) }) .then(resp => resp.json()) .then(json => console.log(json)); 

暂无
暂无

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

相关问题 无法将对象转换为原始值 - 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 vue.esm.js:649 [Vue 警告]:nextTick 中的错误:“TypeError:无法将 object 转换为原始值”vue - vue.esm.js:649 [Vue warn]: Error in nextTick: "TypeError: Cannot convert object to primitive value" vue 将对象转换为URL编码的字符串时无法将对象转换为原始值 - Cannot convert object to primitive value in converting object into URL encoded string Laravel 7.6.2 app.js 错误 app.js:3449 未捕获类型错误:无法将 object 转换为原始值 - Laravel 7.6.2 app.js error app.js:3449 Uncaught TypeError: Cannot convert object to primitive value 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? MULTER-S3:TypeError:无法将对象转换为原始值... diate [as _immediateCallback] - MULTER-S3: TypeError: Cannot convert object to primitive valu…diate [as _immediateCallback] 如何在 GAS 中将原始值转换为日期对象 - How to convert primitive value to Date object in GAS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM