chackerian chackerian - 1 year ago 104
Javascript Question

JS: Variable appears to be a string but displays as: "[object Object]"

I am parsing wikipedia documents and using the npm html-to-text converter package to extract just the text from various wikipedia pages. I am encountering issues when trying to log / send this content to be used on the client side.

Here is my implementation with the npm package:

var stringer = htmltext.fromString(data, {
wordwrap: 130

console.log(stringer) // returns [object Object]
console.log(typeof stringer); // returns string
console.log(util.inspect(stringer)); // returns '[object Object]'

As you can see in the comments, the first console log appears to represent the variable as an object but the second shows that is a string. How can this be?

Answer Source

What are you passing into htmltext.fromString()?

Are you sure you aren't passing an object into there which is converting to string as [object Object]?

For example, if I type the following into a node console I get what you're seeing. :)

 > var htmltotext = require("html-to-text")
 > htmltotext.fromString({})
 '[object Object]'

The usage illustrates that .fromString() expects a string input.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download