I have been trying to use Axios to GET JSON data from a json-server to mimic a database for a project. I am able to print the data inside the .then() portion of the axios block, but I cannot get Axios to return the data to the function so others can use the data from the JSON server request instead of printing it. Also, I'm not using React for this project so I cannot rely on useState's functions. I've looked at so many other solutions and for some reason I just can't get the data out of the promise.
const axios = require("axios")
const query = (id="", table="businesses") => (
axios
.get(`http://localhost:3001/${table}/${id}`)
)
const name = (id) => {
let user = query(id.toString())
return user
.then(res => res.data)
.catch(err => console.log(err))
}
console.log(name(1))
When I try to print the function's returned result, I receive:
server % node dbms.js
Promise { <pending> }
And I expect:
server % node dbms.js
data: {
id: 1,
name: "Carl's Jr.",
address: '123 Flower St.',
}