I am trying to write code to display value of bitcoin in USD, it works fine in console log but when I try to return it to my app.post, it just returns [Object Promise] on screen, I tried the same without using Async and Await and I get the same error, but in console it says Promise Pending
const express = require("express");
const app = express();
const { res } = require("express");
const axios=require("axios");
app.use(express.json());
app.use(express.urlencoded({extended:true}));
app.get("/",function(req,res){
res.sendFile(__dirname+"/index.html");
})
async function getUser(fiat) {
try {
const response = await axios.get("https://api.coindesk.com/v1/bpi/currentprice/"+fiat+".json")
let data = response.data.bpi.USD.rate;
console.log(data);
return data;
}
catch (error) {
console.error(error);
}
}
app.post("/",function(req,res){
var fiat = req.body.fiat;
let answer=getUser(fiat);
res.write(" "+answer);
res.send();
res.end();
});
app.listen(3000,()=>{
console.log("Server Started");
})
You neeed await before calling getUser also make this function async
let answer=await getUser(fiat);