How can I console.log returned data from an NPM module?

Im making an NPM module right now and Im trying to log my returned data.

          async function PokeApi(options) {
              if(options.pokemon) {
                    var baseArgs = baseURL + `/pokemon/${options.pokemon.toLowerCase()}`
                    
                    await fetch(`${baseArgs}`).then(res => res.json()).then(res => {
                        const data = res

                        return data;
                    }).catch(err => {
                        throw new Error("There was an error. Probably because of an invalid pokemon. To fix this issue, try adding dashes between the spaces. \nIf this does not work, try double checking the spelling of the pokemon.")
                    })
              }


          }

  module.exports.PokeApi = PokeApi;

I have this code and in my test.js I have:

const { PokeApi } = require('./index')

PokeApi({
  pokemon: 'pikachu'
})

When I do this, nothing returns because in the actual module, I did, return data;

How would I log this data in the test.js?

Also, If I do something like:

console.log(PokeApi({
  pokemon: 'pikachu'
})
)

I just get
Promise { <pending> }
In the console.

I also tried doing

PokeApi({

  pokemon: 'pikachu',

  experience: 'base',

  types: 'url'

}).then(function(result){

  console.log(result)

})

But I just got undefined in the console.

Other stuff I tried:

const {

  PokeApi

} = require('./index')

async function test() {

await PokeApi({

  pokemon: 'pikachu',

  types: 'all'

})

}

test().then(function(result) {

  console.log(result)

})

returns : undefined

const {

  PokeApi

} = require('./index')

async function test() {

  let uwu = await PokeApi({

    pokemon: 'pikachu',

    types: 'all'

  })

  console.log(uwu)

}

test()

returns: undefined

Also, if I were to do console.log() instead of return it works perfectly fine…

Make it and Async function and use await.

Where should I make it await? @chessebuilderman

Can anyone help???

@HK240 can you try sending a dummy request through Postman and see what kind of stuff comes out?

Or, try:

PokeApi({

  pokemon: 'pikachu',

  experience: 'base',

  types: 'url'

})
.then(res => res.json())
.then(body => console.log(body))

docs: https://www.npmjs.com/package/pokeapi

@CarlyRaeJepsenStan I just get TypeError: Cannot read property 'json' of undefined

@CarlyRaeJepsenStan I also tried with postman.

const {
  PokeApi
} = require('./index')

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  res.send(PokeApi({
    pokemon: 'pikachu',
    types: 'all'
  }))
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})

I also tried adding

                const getPokemon = () => {
                    return data;
                }
                exports.getPokemon = getPokemon;

Then:

const {
  PokeApi,
  getPokemon
} = require('./index')

PokeApi({
    pokemon: 'pikachu'
  })

  console.log(getPokemon)

But it just returned undefined…

I suggesst you to use callback or promises as well. Also, Don’t forget to put Async :

          async function PokeApi(options, callback) {
              if(options.pokemon) {
                    var baseArgs = baseURL + `/pokemon/${options.pokemon.toLowerCase()}`
                    
                    await fetch(`${baseArgs}`).then(res => res.json()).then(res => {
                        const data = res

                        callback(data)
             
                    }).catch(err => {
               
                        throw new Error("There was an error. Probably because of an invalid pokemon. To fix this issue, try adding dashes between the spaces. \nIf this does not work, try double checking the spelling of the pokemon.")
                    })
              }


          }

  module.exports.PokeApi = PokeApi;

To execute it, You can:

const pokeapi = require("pokeapi")
//Example :3 

pokeapi.PokeApi("RANDOM", res => console.log(res))
//Output will Shut into console log

Mark this message as Solution if this helps you out.

Ittt workedd. I just made a little change and did:

const pokeapi = require('./index')

pokeapi.PokeApi({pokemon: 'pikachu'}, res => console.log(res))
1 Like

Well, Solved? haha… Yeah, And Congrats.

Keep it up👍🏻

U could also solve the problem by doing

pokeapi.PokeApi({pokemon: 'pickachu'}).then(res => console.log(res))

since your function returns a Promise because its asynchronous

I prefer .then syntax bcz its cleaner, but whatever works :raised_hands: