Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

238
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda