chackerian
chackerian

Reputation: 1401

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?

Upvotes: 0

Views: 537

Answers (1)

Chris
Chris

Reputation: 2806

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")
 undefined
 > htmltotext.fromString({})
 '[object Object]'
 >

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

Upvotes: 2

Related Questions