Nour Eddein
Nour Eddein

Reputation: 1

Cannot set headers after they are sent to the client?

Here I'm trying to GET data from server, But when I try to open the browser and get the data nothing appear, Its give me the same URL in the browser.

UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]:

// Application Dependencies
require('dotenv').config();
const express = require('express');
const cors = require('cors');
const superAgent = require('superagent');

// Application Setup
const PORT = process.env.PORT || 3000;
const app = express();
app.use(cors());

//KEYS
const WEATHER_API_KEY = process.env.WEATHER_API_KEY;
const GEOCODE_API_KEY = process.env.GEOCODE_API_KEY;
const PARK_KEY = process.env.PARK_KEY;

//Route Definitions
app.get('/location', locationHandler);
app.get('/weather', weatherHandler);
app.get('/parks', parksHandler);
app.get('*', errorHandler);

//Location Handler
async function locationHandler(req, res) {
    try {
        console.log(req.query);
        let getCity = req.query.city;
        let url = `https://us1.locationiq.com/v1/search.php?key=pk.9e079e96352c63d18cf387532fa6b9ad&q=seattle&format=json`;
        const locationData = await superAgent.get(url);
        const apiData = JSON.parse(locationData.text);
        console.log(superAgent.get(url))
        res.send(superAgent.get(url));
        // let aaa = new City(getCity, apiData[0].display_name, apiData[0].lat, apiData[0].lon);
        // console.log(aaa);
        res.status(200).send(new City(getCity, apiData[0].display_name, apiData[0].lat, apiData[0].lon));
    } catch (error) {
        res.status(404).send('Something went wrong in LOCATION route')
    }
}

Upvotes: 0

Views: 748

Answers (1)

Laurent Dhont
Laurent Dhont

Reputation: 1062

You cannot send a response more then once. So remove this line: res.send(superAgent.get(url));

Upvotes: 0

Related Questions