Click here to Skip to main content
15,745,005 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()

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

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

   //Define base url
   let apiUrl=''
   //let apiUrl=''

  //call api and return response


  .then(response =>; {
      //console.log("test" +response);
     res.json({  coins: })
   .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)
  console.log("This one " +data)

  // component loads
  useEffect(() =>; {
  }, [])

  return (

<button onClick={fetchCoins}>Fetch Coins</button>

What I have tried:

Its working for url but, not working for the URL

I tried different formats for res.json syntax. In react app I tried the map function as well to display data, but nothing worked.
Updated 10-Mar-21 3:46am
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