Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

241
Vistas
How to fetch data from an api and save to mongodb using mongoose

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.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

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

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda