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…