I am have a lambda function which calls an API and return the data to react where the react prints it in the front end.
When I call an API(1) it is working , but for another API its not working. Can someone please help me resolve this?
Lambda function code:
var express = require('express')
var bodyParser = require('body-parser')
var awsServerlessExpressMiddleware = require('aws-serverless-express/middleware')
var app = express()
app.use(bodyParser.json())
app.use(awsServerlessExpressMiddleware.eventContext())
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*")
res.header("Access-Control-Allow-Headers", "*")
next()
});
const axios=require('axios')
app.get('/coins', function(req, res) {
let apiUrl='https://bismarck.sdsu.edu/api/instapost-query/service-calls'
axios.get(apiUrl)
.then(response =>; {
res.json({ coins: response.data })
})
.catch(err =>; res.json({ error: err }))
})
React code:
import React, { useState, useEffect } from 'react'
import { API } from 'aws-amplify'
import './App.css';
function App() {
const [coins, updateCoins, data] = useState()
const [input, updateInput] = useState({ limit: 5, start: 0 })
function updateInputValues(type, value) {
updateInput({ ...input, [type]: value })
}
async function fetchCoins() {
const data = await API.get('cryptoapi', '/coins')
console.log("before" +data)
updateCoins(data.coins)
console.log("This one " +data)
}
useEffect(() =>; {
}, [])
return (
<div>
{
<h5>{JSON.stringify(coins)}</h5>
}
<button onClick={fetchCoins}>Fetch Coins</button>
</div>
);
}
What I have tried:
Its working for url
https://api.coinlore.com/api/tickers/?start=3&limit=2
but, not working for the URL
https://bismarck.sdsu.edu/api/instapost-query/service-calls
.
I tried different formats for res.json syntax. In react app I tried the map function as well to display data, but nothing worked.