Warm tip: This article is reproduced from serverfault.com, please click

Pass data to MongoDB after Node's GET request

发布于 2020-11-21 19:40:00

Exercise is quite straightforward. I need to move data obtained after evoking Node' http.get() to mongoDB. I'm using mongoose for that. Issue I have is how to pass the GET result into mongoose's methods.

Couple questions regarding the approach:

  1. Should express.js be also used for this in order to establish a localhost server that would serve as a proxy?
  2. Is my current approach valid from good-practices point of view?
  3. How to automate whole task? Simply with CRON to trigger the script?

Currently I'm stuck with below:

Sample API data:

{
  activity: 'Memorize the fifty states and their capitals',
  type: 'education',
  participants: 1,
  price: 0,
  link: '',
  key: '4179309',
  accessibility: 0
}

structure of my mongo collection

const mongoose = require('mongoose');

const activityapiSchema = mongoose.Schema({

  _id: mongoose.Schema.Types.ObjectId,
  activity: {type: String, required: true},
  type: {type: String, required: true},
  key: {type: Number, required: true}
});

module.exports = mongoose.model('ActivityAPI', activityapiSchema);

main code

const https = require('https');
const mongoose = require('mongoose');

const options = new URL('https://www.boredapi.com/api/activity');

//connect to MongoDB
mongoose.connect('CONNECTION_STRING',
{
  useNewUrlParser: true,
  useUnifiedTopology: true
})
.then(() => console.log("MongoDb connected"))
.catch(err => console.log(err));

//obtain data using GET
https.get(options, (res) => {
  console.log('statusCode:', res.statusCode);
  //console.log('headers:', res.headers);

  res.on('data', (data) => {
    //process.stdout.write(d)
    //display returned data by API
    console.log(JSON.parse(data));
  });
})

.on('error', (err) => {
  console.error(err);
});

//passing the result into MongoDB, need help
Questioner
marcin2x4
Viewed
0
marcin2x4 2020-11-29 19:13:13

I found a workaround code to pass GET result into next line of code.

//obtain data using GET
https.get(options, (res) => {
  console.log('statusCode:', res.statusCode);
  res.on('data', (data) => {
    callback(data); //allows to reference the data in callback function below
  });
})

.on('error', (err) => {
  console.error(err);
})
;

//retrive data and load
function callback(value){
   console.log(JSON.parse(value));
   let valueMod = JSON.parse(value);

//next thing you want to do with the data...

};