Click here to Skip to main content
15,881,139 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
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')

// declare a new express app
var app = express()
app.use(bodyParser.json())
app.use(awsServerlessExpressMiddleware.eventContext())

// Enable CORS for all methods
app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*")
  res.header("Access-Control-Allow-Headers", "*")
  next()
});


/**********************
 * Example get method *
 **********************/
 //Import axios
 const axios=require('axios')
app.get('/coins', function(req, res) {

   //Define base url
   let apiUrl='https://bismarck.sdsu.edu/api/instapost-query/service-calls'
   //let apiUrl='https://api.coinlore.com/api/tickers/?start=3&limit=2'


  //call api and return response

  axios.get(apiUrl)

  .then(response =>; {
      //console.log("test" +response);
     res.json({  coins: response.data })
   })
   .catch(err =>; res.json({ error: err }))
})

React code:
// Import useState and useEffect hooks from React
import React, { useState, useEffect } from 'react'

// Import the API category from AWS Amplify
import { API } from 'aws-amplify'

import './App.css';

function App() {

  // Create additional state to hold user input for limit and start properties
  const [coins, updateCoins, data] = useState()
  const [input, updateInput] = useState({ limit: 5, start: 0 })

  // Create a new function to allow users to update the input values
function updateInputValues(type, value) {
  updateInput({ ...input, [type]: value })
}

// Update fetchCoins function to use limit and start properties
async function fetchCoins() {
  const data = await API.get('cryptoapi', '/coins')
  console.log("before" +data)
  updateCoins(data.coins)
  console.log("This one " +data)
}

  // component loads
  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.
Posted
Updated 10-Mar-21 3:46am
v3
Comments
Richard Deeming 10-Mar-21 9:48am    
You need to check the documentation for the service you're trying to call, and check the response for errors. If you can't work it out, then contact the support team for the service that isn't working.

Nobody here can see what errors you're getting, nor the documentation for the "not working" service.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900