E
E
Evgeny Nikolaev2017-03-04 10:46:01
JavaScript
Evgeny Nikolaev, 2017-03-04 10:46:01

How to call model method from NodeJS controller?

The essence of this is a simple data model

exports.CurrencyModel = function(name2,value2,date2){
  var name = name2, date = date2, value = value2;
  console.log(name)


  function getName(){
    return name;
  }

  function getDate(){
    return date;
  }
  function getValue(){
    return value;
  }

  function setName(name1){
    name = name1;
  }

  function setDate(date1){
    date = date1;
  }

  function setValue(value1){
    value = value1;
  }
  
}

The constructor of which accepts and sets values.
And there is a controller
var http = require('http');
var c = require('../models/CurrencyModel')
exports.CurrencyController = function(){

  

  var options = {
    host: 'resources.finance.ua',
    path: '/ru/public/currency-cash.json'
  };

  http.get(options, function(resp){
    var str = "";
    resp.on('data', function(chunk){
      str += chunk;

    });
    resp.on("end", function(){
      var response = JSON.parse(str)
      //console.log(response.organizations[0].currencies)
      var arra = ["EUR", "RUR", "USD"];
      var objC = response.organizations[0].currencies[arra[0]];
      var model = new c.CurrencyModel(arra[0], objC.ask, "fs");
      console.log(model.getName())
    })
  }).on("error",function(e){
    console.log("Got error:" +e.message)
  })


}

Which makes a specific request, on line 23 I create an instance of the model, and on line 24 I try to call a getter, but it tells me in response that getName is not a function. I can't deal with this problem. One person solved it and said something about "self.getName = function etc" but I didn't find anything about it. Genius help me

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
lemme, 2017-03-04
@khveugen_27

this.getName = function() {}
Your choice. =)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question