-1

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?

Simon D
  • 29
  • 1
  • 6
  • You have several issues: `res.body` must be `req.body`, you must iterate over array asynchronously (use `async` package), send result after saving all data – Medet Tleukabiluly Jun 05 '16 at 07:15

1 Answers1

0

You have several issues:

  1. res.body must be req.body
  2. you must iterate over array asynchronously (use async package)
  3. send result after saving all data

See the sample below

router.get('/', function(req, res) {

    // npm install async --save
    var async = require('async');
    request.get('url', function (error, response, body) {
        if (error && response.statusCode !== 200) {

            // request could not be completed
            return res.send(400, {message: 'Something went wrong'});
        } else {

            // get the count
            var iterator = body.id || body.num_items;

            // iterate number of times
            async.times(iterator, function(number, next){

                // create your new object
                var newItem = Item({
                    id: number,
                    product_name: ,
                    app: 'appName',
                    quantity: req.body.quantity, // <== not res.body
                    price: req.body.price,
                    icon_url: req.body.,
                    name_color: req.body.,
                    quality_color: body.,
                    created_at: Date.now
                });

                // save and call next (which is callback)
                newItem.save(next);
            }, function(timesErr, timesResult){

                // if something failed, return error
                // even if 1 item failed to save, it will return error
                if(timesErr) {
                    return res.send(400, {message: 'Something went wrong'});
                }
                // send the list of saved items;
                // or whatever you want
                res.status(200).send(timesResult);
            });
        }
    });
});
Medet Tleukabiluly
  • 11,662
  • 3
  • 34
  • 69
  • Thanks for that, i tried to log everything which could help me recieve those body params but everything was undefined. I used req.body before thats why im at the end. Even though i have body-parser as dependency. Here is a output example `{ __v: 0, id: "0", appid: 730, updated_at: "2016-06-05T16:18:49.445Z", _id: "575450e9a3ad44e02a08615c", created_at: "2016-06-05T16:18:49.408Z" }, { __v: 0, id: "1", appid: 730, updated_at: "2016-06-05T16:18:49.469Z", _id: "575450e9a3ad44e02a08615d", created_at: "2016-06-05T16:18:49.430Z" }` As you can see i cant access the data in the json body – Simon D Jun 05 '16 at 16:21
  • This is another question, open new one as this doesn't fit to current Post your provided – Medet Tleukabiluly Jun 05 '16 at 16:49