Santhosh Veer
Santhosh Veer

Reputation: 83

json.map is not a function

json.map is not a function - Getting this Error while using Node-fetch

GET Method

JSON Fetch result

{"status":200,"email":"[email protected]","domain":"example.com","mx":false,"disposable":false,"alias":false,"did_you_mean":null,"remaining_requests":99}

I want to Print this on Node CLI Table

    const printContent = json => {
    console.log()

      const group = json.map(g => [
        g.status,
        g.email
      ])

      const table = new Table({
        head: ["Name", "Email"],
        colWidths: [20, 20]
      })

      table.push(...group)
      console.log(table.toString())
  }

Upvotes: 2

Views: 16605

Answers (2)

Dajer
Dajer

Reputation: 108

You need to parse the JSON first in order to get a JS object. However the map() function works on arrays. I would simply just do it without map, since you do not fetch an array:

let obj = JSON.parse(json);
const group = [obj.status, obj.email];

Upvotes: 0

Steven Spungin
Steven Spungin

Reputation: 29109

map only works on arrays. Is json a single object? If so, try:

  const group = [json].map(g => [
    g.status,
    g.email
  ])

That will return a dual element array with your mapped object.

If you want an object, try:

const group = Object.assign({}, {status:g.status, email:g.email}) 

Upvotes: 7

Related Questions