0

I’m very new to node.js and I have a problem accessing data from a JS file. I have 3 files (see below) I'm storing data in “SeondController” that I want to retrieve from “FirstController”. I can reach “FirstController” just fine from “index.js”, but I can’t get the data from “SeondController”.

I sure it’s something simple, but I can’t figure it out. I’m using Express 4, and think it has something to do with routing, but again I’m not sure. The code is running on the Heroku cloud if that helps.

The error is something like SecondController.getData is not a function.

Could someone please point out my error.

————————————————————————— index.js —-

var express = require('express');
var app = express();
var router = express.Router();
var path = require('path');
var FirstController = require('./FirstController');
var SecondController = require('./SecondController');
var bodyParser = require('body-parser')
app.set('port', (process.env.PORT || 5000));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

app.get('/getObjects’, function(req, res) {
    res.setHeader("Access-Control-Allow-Origin","*");
    FirstController.getObjects(req, res); 
});

app.listen(app.get('port'), function() {
  console.log('Node app is running on port', app.get('port'));
});

——————————————————————— FirstControlle.js —-

var SecondController = require('./SecondController');
function FirstController() {
}

FirstController.getObjects = function(req, res) { 
  var returnObjects =  SecondController.getData(req, res);     
};

module.exports = FirstController;

——————————————————————— SecondControlle.js —-

function SecondController() {
}

var messageData = {
        "attachment": {
            "type": "template",
            "payload”:”12345”
         }
}

SecondController.getData= function(req, res) { 
  return messageData;    
};

module.exports = SecondController;
user3783935
  • 51
  • 1
  • 4
  • You've got multiple spelling mistakes in file and instance names. Also there are wrong quotes in object definition in "SecondController". When you ask the question in this form it is not clear if these mistakes are not the reason – Kirill Slatin May 12 '16 at 20:31
  • Sorry if there are spelling errors but I wrote this code to represent the problem I'm having. My original code is much larger and I didn't want to paste all the code. If you look pass any spelling errors it would be appreciated – user3783935 May 12 '16 at 21:37
  • As it is not original code, you must be missing some detail. In posted code I can't find a possible reason for the error that you describe – Kirill Slatin May 13 '16 at 00:03
  • I found the problem. (User error); The code above is correct expect for syntax and spelling errors. My original – user3783935 May 13 '16 at 19:45
  • I found the problem. (User error); The code above is correct expect for syntax and spelling errors. My original code didn't declare "SecondController.getData", I had only declared a function name getData, leaving off the "SecondController" part. @Kirill Slatin thanks for your support. – user3783935 May 13 '16 at 19:49
  • You're welcome, glad it helped – Kirill Slatin May 13 '16 at 21:26

0 Answers0