最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - TypeError: Cannot convert object to primitive value - Stack Overflow

programmeradmin4浏览0评论

I have a texteditor (div) and there's a function to format the (selected)text inside it. When I mark a part of the text and chose to make it look like this (code snippet) I have to use   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   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>):

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; with a space without having to convert html to (string) in order to avoid this error?

I have a texteditor (div) and there's a function to format the (selected)text inside it. When I mark a part of the text and chose to make it look like this (code snippet) I have to use &nbsp; 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 &nbsp; with a space before sending it to the server.

What I did was the following

// replace &nbsp; by " "
let content = $('.editor').html().replace(/(&nbsp;)/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>):

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; with a space without having to convert html to (string) in order to avoid this error?

Share Improve this question asked Jan 2, 2019 at 22:22 FeelsbadmanFeelsbadman 1,1654 gold badges19 silver badges40 bronze badges 6
  • Instead of guessing how to work around a problem, why not include the code that produces the TypeError, indicating in which line it occurs? – trincot Commented Jan 2, 2019 at 22:26
  • NodeJS does not show where the error occured all I know is which file it is. Do you know of any way to enable some sort of debugging mode in node? – Feelsbadman Commented Jan 2, 2019 at 22:28
  • 1 But you can debug the code. Find out where it produces that error. There are many ways to do so. – trincot Commented Jan 2, 2019 at 22:30
  • 1 Don't be shy, feel free to let me know about those ways – Feelsbadman Commented Jan 2, 2019 at 22:32
  • 2 It is not a matter of shyness. You should take the initiative and look for yourself. A simple search on the internet leads to this: nodejs/en/docs/guides/debugging-getting-started – trincot Commented Jan 2, 2019 at 22:34
 |  Show 1 more ment

1 Answer 1

Reset to default 2

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.

As far as I understand, you can't pass the string &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. 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.

Encode your strings using encodeURIComponent, or POST your data with a different encoding (for example, 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:

let data = {
  userId: 1,
  id: 1
}

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

发布评论

评论列表(0)

  1. 暂无评论