after getting the basics of how to work with node.js and the mean stack i like to start with my own project.
The Problem is:
I´m not able to access the json body/attributes of a requested API which i like to save to mongodb and update every 24h automatically.
What im trying:
Is to call 2 API´s. Theses response with a json list of datasets which i try to merge by the same name into my mongoose Schema. Using body.param or response.param, didnt work.
Example responses of a get request:
url1/api/products/all
{
data: [
{
id: 1,
product_name: "Product1",
app: appName,
quantity: 137360,
price: 0.05,
updated_at: "2016-06-04 23:02:03",
median_week: 0.04,
median_month: 0.05,
},
.....
{
id:142640
...
}
}
url2/api/item/all?key=apikey
{
success: true,
num_items: 6713,
products: [
{
product_name: "Product1",
....
icon_url: "//url/economy/image/bla.png",
product_color: "8650AC",
quality_color: "EB4B4B"
},
....
After finding out that express only routes to local urls, im using request now.
Thats my routes/item.js
var express = require('express');
var qs = require('querystring');
var request = require('request');
var _ = require('underscore');
var router = express.Router();
var Item = require('../models/item');
var options = {
url: 'url2' + ?key + app.config.url2.apikey ,
port: 80,
method: 'GET',
json:true
}
/* GET listing. */
router.get('/', function(req, res) {
request.get('url', function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body) // Prints JSON Body
//Iterate through each id of url1 and num_items of url2 and update collection
// Better to make another request method just to call the second api?
for each(item in body.id || body.num_items) {
var newItem = Item({
id: item[i],
product_name: ,
app: 'appName',
quantity: res.body.quantity,
price: res.body.price,
....
icon_url: res.body.,
name_color: res.body.,
quality_color: body.,
created_at: Date.now,
updated_at:
})
newItem.save(function(err) {
if (err) throw err;
console.log('Item created');
})
//return res.json(body);
}
}
res.sendStatus('304');
});
});
This is my Item Model
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ItemSchema = new Schema({
id: String,
....
created_at: {
type: Date,
default: Date.now
},
updated_at: Date
});
var Item = mongoose.model('Item', ItemSchema);
ItemSchema.pre('save', function(next) {
var currentDate = new Date();
this.updated_at = currentDate;
if (!this.created_at)
this.created_at = currentDate;
next();
});
module.exports = Item;
After writing this question, i think that isnt the best way to approach this. Is there any best practice?