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:
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
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...
};