Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

237
Views
Cómo obtener datos de una API y guardarlos en mongodb usando mongoose

Así es como he definido mi esquema.

 const apiSchema = new Schema({ entries: [{ API: { type: String }, Description: { type: String }, Link: { type: String }, Category: { type: String } }] }); module.exports = mongoose.model('api', apiSchema);

Y aquí está mi controlador

 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); }); }); });

Cuando ejecuto el servidor y abro la brújula, encuentro que solo se guarda la identificación del objeto en la matriz de entradas. Quiero que todos los campos de la matriz de entradas se guarden en la base de datos.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Puede guardar los datos obtenidos de la siguiente manera:

  1. primero importe el módulo HTTPS para enviar la solicitud de obtención de HTTPS
  2. crear una matriz para mantener fragmentos de búfer
  3. Cuando todos los fragmentos se hayan recibido por completo, concatenar estos fragmentos
  4. guardar datos concatenados en la base de datos
 //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); }) }) };

resultado

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!