Typeerror first argument must be a string html-react-parser

There are several problems in the code.

  1. You get TypeError: First argument must be a string or Buffer because you are trying to send boolean value false in form data -- HTML form does not support boolean value. In HTML, checked checkbox will send its value, while unchecked checkbox won't.

    To fix the issue, you can change false to 'FALSE'[string] and parse it in server side.

  2. The use of fs.readFile['retext.png', 'utf8'] is incorrect. To attach file in the form, the right way is: imageFile: fs.createReadStream['retext.png'].

  3. When formData: formData is used in request.post[...], the Content-Type of the HTTP request would be multipart/form-data automatically, you don't need to define Content-Type header again.

    Moreover, it is incorrect to set json: true, which will make Content-Type as application/json. This conflict will make request module confused, and may cause problem in some JavaScript environment.

  4. The callback function function[err, resp, body]{...} should be part of request.post[...], maybe it is a typo.

In summary, the correct code would look like:

var formData = {
  name: 'TestDeck',
  description: 'This is a test deck for my api',
  private: 'FALSE',
  shareable: 'FALSE',
  ttsLanguages: [],
  blacklistedSideIndices: [],
  blacklistedQuestionTypes: [],
  gradingModes: [],
  imageAttribution: '//www.logogarden.com/wp-content/uploads/lg-index/Example-Logo-6.jpg',
  imageFile: fs.createReadStream['retext.png']
}

function createDeck[connection] {
  request.post[{
    url: '',
    formData: formData
  }, function[err, resp, body] {

  }]
}

Traceback:

esm.js?1aee:1 Uncaught [in promise] Error: markdown-to-jsx: the first argument must be
                             a string
    at compiler [esm.js?1aee:1]
    at Component.Markdown [as constructor] [esm.js?1aee:1]
    at Component.doRender [as render] [preact.js?10a9:229]
    at renderComponent [preact.js?10a9:271]
    at setComponentProps [preact.js?10a9:248]
    at buildComponentFromVNode [preact.js?10a9:339]
    at idiff [preact.js?10a9:134]
    at innerDiffNode [preact.js?10a9:176]
    at idiff [preact.js?10a9:152]
    at diff [preact.js?10a9:109]

I am a JS beginner, so apologies if there is an easy fix for this. I have a Preact project that I'm trying to render markdown on, and I'm passing a string in according to the documentation. I've performed the preact-compat redirect and other react packages are working as expected. Here is example code:

import {h, render} from 'preact';
import style from './style.scss';
import Markdown from "markdown-to-jsx";
import About from './about.md';

export default function AboutPage[] {
    console.log[About];
    console.log[typeof [About]];
    return [
        
] }

I have also tried {About} and TEST STRING but receive the same error.

I was creating an npm module with the help create-react-library,
I which i was using html-react-parser.
but after importing html-react-parser, its giving error

[!] Error: Unexpected token
node_modules/entities/maps/entities.json [1:9]
1: {"Aacute":"\u00C1","aacute":"\u00E1","Abreve":"\u0102","abreve":"\u0103","ac":"\u223E","acd":"\u223F","acE":"\u223E\u0333","Acirc":"\u00C2","acirc":"\u00E2","acute":"\u00B4","Acy":"\u0410","acy":"\u0430","AElig":"\u00C6","aelig":"\ ....very long json 

error Command failed with exit code 1.

Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. [Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.]

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

2 Answers

Christian Higgins June 7, 2018 7:32pm

James, I was having the same problem because I had stopped watching the video too soon. Andrew also has an error in the video starting about 4:15. I'm seeing that you're missing the

that he ends up adding in.

Jonathan McWilliams September 19, 2018 7:15pm

I had that error when I did not have this line of code correct in the renderer.js, I think I had just readFile instead of readFileSync

 var fileContents = fs.readFileSync['./views/' + templateName + '.html'];

Chủ Đề