Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"TypeError: Cannot read property 'apply' of undefined"

Using node, express, socket.io, jade and angular. Getting the error: TypeError: Cannot read property 'apply' of undefined. Any suggestions?

index.js:

module.exports = function(app, res) {
  res.render('index', { title: 'Express' });
  var io = app.get('io');
  io.on('connection', function(socket){
  });
};

index.jade:

extends layout

block content

script.
    var app = angular.module('hackigur', []);
    var socket = io.connect();
    var refreshTimer = 10;

    app.controller('UpdateController', function($scope){
        //socket.on('update', function(msg){
            //$scope.refreshTimer = msg;
            //$scope.$apply();
        //});

        setInterval(secondTick,1000);

        function secondTick() {
            if(refreshTimer != 0) {
                refreshTimer -= 1;
            }
            $scope.refreshTimer = refreshTimer;
            $scope.$apply();
        };
    });

h1= title
p Welcome to #{title}

div(ng-controller="UpdateController")
    p(ng-bind="refreshTimer")

layout.jade:

doctype html
html(ng-app="hackigur")
  head
    title= title
    script(src = "/socket.io/socket.io.js")
    script(src = "/js/angular/angular.min.js")
  body
    block content

Full error:

Server listening on port 3000
TypeError: Cannot read property 'apply' of undefined
    at Server.(anonymous function) [as on] (D:\Projects\hackigur\node_modules\so
cket.io\lib\index.js:364:15)
    at module.exports (D:\Projects\hackigur\server\api\index.js:30:8) at ...
like image 449
glog Avatar asked Dec 01 '15 03:12

glog


1 Answers

My router which called my index.js passed app in the module.export as such:

module.export = function (app) {
    app.get( ... , function(..., res) { 
        require(index.js)(app)(res);
};

I needed to declare a variable for app external to my module.export:

var x;
module.export = function (app) {
    x = app;
        app.get( ... , function(..., res) { 
        require(index.js)(x)(res);
};

Don't fully understand why it worked, but it seemed to pass the correct app object to app.get by applying the above.

like image 171
glog Avatar answered Nov 03 '22 07:11

glog