2

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())
  }
4
  • 2
    map works on array, not on object, you're probably looking for Object.keys() or Object.entries() Commented Aug 24, 2018 at 11:37
  • Post Updated... Commented Aug 24, 2018 at 11:38
  • Can you show the rest of your code where you actually get and parse the JSON? I'm guessing you're calling .map on the string not a JS object. Commented Aug 24, 2018 at 11:38
  • https://www.validator.pizza/email/[email protected] Commented Aug 24, 2018 at 11:40

2 Answers 2

7

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}) 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks perfect it's Working :)
0

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];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.