Friday, 17 March 2017

Nodes API request

I would like do the following:

  1. create new Object "Evaluation"

  2. Get data from aclient and save it to the object

  3. Make a post request with data from the "Evaluation" object

  4. Add request response to the "Evaluation" object

  5. Save to mongo Db

Alls looks good, but I cant add the response data from the post request to the Object "Evaluation". I can access it. But I cant add additional data from the second request to it.

 res.on("end", function () {
    //console.log(body.toString());
  body =  JSON.parse(Buffer.concat(chunks));

console.log(body.value) // does work

    evaluation.result.date = body.date;  // does not work
    evaluation.result.value = body.value; // does not work

console.log(evaluation.street); //does work


  });

All code:

const express = require('express');
const router = express.Router();
const passport = require('passport');
const jwt = require('jsonwebtoken');
const config = require('../config/database');
const User = require('../models/user');
const Evaluation = require('../models/evaluation');
//Load the request module
var request = require('request');

var evaluation = new Evaluation();
var body;



router.post('/evaluation', (req, res, next) => {
  evaluation.street =req.body.street;
  evaluation.house_number =req.body.house_number;
  evaluation.town =req.body.town;
  evaluation.zip =req.body.zip;
  evaluation.category =req.body.category;
  evaluation.type =req.body.type;
  evaluation.year =req.body.year;
  evaluation.living_area =req.body.living_area;
  evaluation.ground_area =req.body.ground_area;
  evaluation.elevator =req.body.elevator;
  evaluation.garages =req.body.garages;



var http = require("https");
var options = {
  "method": "POST",
  "hostname": "",
  "port": null,
  "path": "",
  "headers": {
    "content-type": "application/json",
    "authorization": "",
    "cache-control": "no-cache",
    "postman-token": "1d886874-2932-d64c-1f23-a62d657f75a2"
  }
};

var req = http.request(options, function (res) {
  var chunks = [];

  res.on("data", function (chunk) {
    chunks.push(chunk);
  });

  res.on("end", function () {
    //console.log(body.toString());
  body =  JSON.parse(Buffer.concat(chunks));

console.log(body.value) // does work

    evaluation.result.date = body.date;  // does not work
    evaluation.result.value = body.value; // does not work

console.log(evaluation.street); //does work


  });
});


if (evaluation.type == "ETW") {

  req.write(JSON.stringify({
    category: 'ETW',
    date: '2017-03-02',
    address:
     { nation: 'DE',
       street: evaluation.street,
       house_number: evaluation.house_number,
       zip: evaluation.zip,
       town: evaluation.town },
    construction_year: evaluation.year,
    living_area: evaluation.living_area,
    elevator: evaluation.elevator,
    garages: evaluation.garages }));

}

else {

  req.write(JSON.stringify({ category: 'EFH',
  date: '2017-02-17T00:00:00',
  address:
   { nation: 'DE',
     street: evaluation.street,
     house_number: evaluation.house_number,
     zip: evaluation.zip,
     town: evaluation.town },
  construction: evaluation.category,
  construction_year: evaluation.year,
  living_area: evaluation.living_area,
  plot_area: evaluation.ground_area,
  garages: evaluation.garages }));




}



req.end();



evaluation.save(function (err) {
  if (err) return handleError(err);
  // saved!
})
res.json({"message": "Success"});
});



router.get('/evaluation', (req, res) => {
    Evaluation.getAll((err, evaluations) => {
        if(err){
            throw err;
        }
        res.json(evaluations);
    });
});








module.exports = router;

Schema:

const mongoose = require('mongoose');
const config = require('../config/database');

// User Schema
const EvaluationSchema = mongoose.Schema({
  street: {
    type: String,
    required: true
  },
  house_number: {
    type: String,
    required: true
  ,
  },
  town: {
    type: String,
    required: true
  },
  zip: {
    type: String,
    required: true
  },

  living_area: {
    type: String,
    required: true
  },

  year: {
    type: String,
    required: true
  },

  category: {
    type: String,

  },

elevator: {
    type: String,

  },

garages: {
      type: String,
      required: true
    },

    ground_area: {
        type: String

      },


      result: {
        date: {type: String},
        value: {type: String},
        meta: {
          standarderror: {type: String},
          score: {type: String},
          range: {
            min: {type: String},
            max: {type: String}
          }
        },
        coordinates: {
          lat: {type: String},
          lng: {type: String},
        }

    }



});

const Evaluation = module.exports = mongoose.model('Evaluation', EvaluationSchema);


// Get Books
module.exports.getAll = (callback, limit) => {
    Evaluation.find(callback).limit(limit);
}



via Fabian Tschullik

No comments:

Post a Comment