getting photos from one endpoint and saving in another one – nodejs

Basically what I want to do is get photos from another endpoint in spacex API. The photos are on endpoint rockets/rocket_id, and im trying to get them but always gets an empty values.
spaceX api is someone want to see it : https://docs.spacexdata.com/

api = require("./api")
// ...
server.get('/rockets', async (req, res) => {
    try {
        const { data } = await api.get('/v3/launches');
        var lista = [];

        for (var i = 0; i < data.length; i++) {
            const img = api.get('/v3/rockets/' + data[i].rocket['rocket_id']);
            lista.push({
                name_rocket: data[i].rocket['rocket_name'],
                first_stage: data[i].rocket['first_stage']['cores'][0]['core_serial'],
                second_stage: data[i].rocket['second_stage']['payloads'][0]['payload_mass_kg'],
                link_patch: data[i]['links']['mission_patch'],
                link_video: data[i]['links']['video_link'],
                details: data[i]['details'],
                launch_site: data[i]['launch_site']['site_name'],
                img: img
            });
        }
        return res.send(lista);
    } catch (error) {
        res.send({ error: error.message });
    }
});

server.get('/link', async (req, res) => {
    try {
        const { data } = await api.get('/v3/rockets/falcon9');
        var lista = [];

        lista.push({
            imagem: data['flickr_images']
        });

        return res.send(lista);
    } catch (error) {
        res.send({ error: error.message });
    }
});

api.js:

const axios = require("axios");
const api = axios.create({ baseURL: 'api.spacexdata.com', });

module.exports = api;

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

The issue is coming from the URL. You need “http://” or “https://”.

axios.create({ baseURL: 'https://api.spacexdata.com'})

To debug this in the future, consider a try/catch and some logging. The console.log(err) below gave me a much more detailed error that I was able to google.

try{
  const result = await api.get(...)
  console.log(result)
catch (err) {
  console.log(err)
}

Related: StackOverflow issue


In case these were also causing problems, it looks like you’re missing an await and some object drilling here:

const img = api.get('/v3/rockets/' + data[i].rocket['rocket_id']);

Should be changed to this:

const { data } = await api.get('/v3/rockets/' + data[i].rocket['rocket_id']);
const imgs = data.flickr_images
  1. You could probably use GET /rockets once instead of GET /rockets/:id in a loop in the first endpoint for performance.
  2. v3 of this API is deprecated. Would suggest moving to v4.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x