Home > Software engineering >  How to fetch data from an api and save to mongodb using mongoose
How to fetch data from an api and save to mongodb using mongoose

Time:05-17

This is how I have defined my schema

const apiSchema = new Schema({
    entries: [{
        API: {
            type: String
        },
        Description: {
            type: String
        },
        Link: {
            type: String
        },
        Category: {
            type: String
        }

    }]
});

module.exports = mongoose.model('api', apiSchema);

And here is my controller

const Data = require('./models/api');
p.get('/', (req, res, next) => {
    request('https://api.publicapis.org/entries', function (error, response, body) {
        var result = JSON.parse(body);
        console.log('body:', result); 
        

result = new Data({
            entries: {
                API: req.body.API,
                Desription: req.body.Desription,
                Link: req.body.Link,
                Category: req.body.Category   
            }
         })
        result.save()
            .then(result => {
                console.log('Entry saved');
            })
            .catch(err => {
                console.log(err);
            });
        
    });
    
});

When I run the server and open compass I find that only the object id in the entries array gets saved. I want all the fields in the entries array to be saved in the database.

CodePudding user response:

You can save the fetched data as follows:

  1. first import the HTTPS module to send the HTTPS get request
  2. create an array to keep buffer chunks
  3. When all chunks are completely received, concat these chunks
  4. save concatenated data on the DB
//test.js controller file
const Data = require('./../database/models/test');
const https = require('https');

module.exports = (req,res)=>{
    let data =[]; //array to keep data chunks
    
    https.get('https://api.publicapis.org/entries', function (response) { //send  request to api
        
  response.on('data', d => {
    data.push(d); //get data as chunk by chunk and push them to array
  }).on('error',e=>{
      console.log(e); //
  });

  response.on('end',()=>{
//when all data chunks are received, 
// concat all buffered chunks and 
//create js object from it
   let fetchedData= JSON.parse(Buffer.concat(data).toString()); 
   console.log(fetchedData);
   let result = new Data({
    entries:fetchedData.entries // fetched data has entries array , that is data that you want to save
});
result.save() //save data to db
    .then(result => {
        console.log('Entry saved');
    })
    .catch(err => {
        console.log(err);
    });

    res.send(result);
  })

      
}) };

result

  • Related