简体   繁体   中英

Axios.all in NodeJS failing with 404 error

I hope you can help me because it is HOURS of trying to get this problem resolved. I've googled so much and tried all of the solutions I found, but I keep getting the same error. I am trying to make an axis get a request to an API that is paginated for 1 result per page, loop through all of the results, and resolve the promises with the promise array. I have verified that without the loop, just getting 1 request, everything works. I have successful writing to MongoDB using MongoDB driver and its fine. Once I bring the loop in I cannot get the promises to resolve. I was able to console.log that the promise array does, indeed, have x number of pending promises in them.

const MongoClient = require('mongodb')
const axios = require('axios');
const url = 'https://catfact.ninja/fact'

let db = null;
let client = null;

//this one works great
const getMetaData = function () {  
    let data = axios.get(url+"s")
    .then(response => {
        return response.data
    }).catch(error => console.log(error));
   return data;
}
//this one will not resolve
const dataArray =  async function (total) {
    let baseUrl = url+"s/facts?page="
    let res =[];    
    let promises = [];
    for (let page = 1; page <= total; page++){
        promises.push(axios.get(baseUrl+page))
    }
    axios.all(promises).then(result => console.log(result))
   //originally i wanted to map the result to an array of json
  //objects, but if i could even get a console.log it would be
 //a win. spread operator not working, Promise.all not working
 //i must have tried 12 different stackoverflow responses from
//other questions. until i can resolve the promises I can't do anything.


    }








exports.connect = async function(url, done) {
    if (db) return done();

   // let data = await getMetaData()
   // let total = data['total']
    let arr = dataArray(5);


    //console.log("arr is "+arr)
    MongoClient.connect(url, {useNewUrlParser: true}, function (err, client){
        if (err) return done(err);
        client = client;
        db = client.db('morefun');    
        /*
        db.collection('catfacts').insertMany(dataArray, function(err, res){
            if (err) throw err;
            console.log("Inserted: " + res.insertedCount);
        })*/
        done();


    });


}

exports.get = function() {
    return db;
}

//make sure this is correct
exports.close = function(done) {
    if (db) {
        client.close(function(err, result) {
            db = null;
            mode = null;
            done(err);
        });
    }
}

I need an array of JSON objects for the insertMany function to work. please someone help me. what am I doing wrong?

In the for loop, you are creating a URL like this: https://catfact.ninja/facts/facts?page=1 – this is incorrect, the correct URL should be https://catfact.ninja/facts?page=1 (with facts only once).

Also, the keyword async is not needed here, and you should return the result of axios.all .

A correct version of your code:

const dataArray = function (total) {
  let baseUrl = url+"s?page="
  let res =[];    
  let promises = [];
  for (let page = 1; page <= total; page++){
    promises.push(axios.get(baseUrl+page))
  }
  return axios.all(promises).then(result => {
    console.log(result);
    return result;
  });
}

You can then get your data like this:

let arr = await dataArray(5);

Getting the actual data the way you want it

From your comments, I see that what you really want is to post-process the data obtained from the API to ultimately get one array that contains only the cat data.

You can do this by “massaging” the data with map and reduce , like this:

return axios
    .all(promises)
    .then(result => result.map(({ data }) => data.data).reduce((curr, acc) => acc.concat(curr), []));

Note: I've left out the console.log statement here for brevity.

  • The actual data is nested as property 'data' in an object within an object as property 'data', so the map call retrieves that
  • We get an array of arrays, each with an object with cat data; the reduce call flattens this to a simple array of cat data

We get a result that looks like this, which is hopefully what you want 😁:

[
    {
        "fact": "Cats see six times better in the dark and at night than humans.",
        "length": 63
    },
    {
        "fact": "The ability of a cat to find its way home is called “psi-traveling.” Experts think cats either use the angle of the sunlight to find their way or that cats have magnetized cells in their brains that act as compasses.",
        "length": 220
    },
    {
        "fact": "Cat's urine glows under a black light.",
        "length": 38
    },
    {
        "fact": "There are more than 500 million domestic cats in the world, with approximately 40 recognized breeds.",
        "length": 100
    },
    {
        "fact": "A tomcat (male cat) can begin mating when he is between 7 and 10 months old.",
        "length": 76
    }
]

不确定这是否是答案,但我知道当不使用axios想要完全用于axios的语法时,我遇到了问题。

axios.all([fetch1request(), fetch2request()]).then(axios.spread((fetch1, fetch2 ) => * whatever logic you need. But at this point the requests are complete* })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM