2017-02-02 71 views
1

問題は「this.io.emit」イベントを発生させようとするたびに、TypeErrorが発生することです。これは、 "socket.on"ブロックの中にこの文 "this.io.emit"を書き込む間だけ得ます。このブロックの外側に書き込むとエラーは発生しません。TypeError:未定義の 'emit'プロパティを読み取ることができません

これは、他のライブラリを呼び出すためのメインserver.jsファイルです:

const express = require('express'), 
http = require('http'), 
socketio = require('socket.io'); 

class App{ 

constructor() 
{ 
    this.port = process.env.PORT || 81; 
    this.host = `localhost`;   
    this.app = express(); 
    this.http = http.Server(this.app); 
    this.socket = socketio(this.http); 
} 
appConfig(){ 
    new config(this.app); 
} 
appRoutes(){ 
    new routes(this.app,this.socket).routesDefault(); 
} 
appDefault(){ 
    this.appConfig(); 
    this.appRoutes(); 
    this.http.listen(this.port,this.host,()=> { 
     console.log(`Listening`); 
    }); 
}} 

私のサーバー側のコードは次のとおりです。

'use strict'; 
class Routes { 

constructor(app,socket) { 
    this.app = app; 
    this.io = socket; 
    this.users=[]; 
} 

routesTemplate() 
{ 
    this.app.get('/',function(req,res){ 
     res.render('index'); 
    }); 
} 

socketEvents() 
{ 
    this.io.on('connection',(socket) => { 
     socket.on('send message',function(data) 
     { 
      this.io.emit('new message',data);//here the error lies. 
     }); 
    }); 
} 
routesDefault() 
{ 
    this.routesTemplate(); 
    this.socketEvents(); 
}} 
module.exports = Routes; 

私はまた、「this.users.The長さにアクセスしようとしました"socket.onステートメント内で、同じTypeErrorを生成しました。プロパティの長さを読み取ることができません。なぜそれが起こっているのか分かりません。この問題を解決するのを手伝ってください。

クライアント側:

 <script> 
     $(function($){ 
      var socket = io.connect(); 
      var $messageForm = $('#send-message'); 
      var $messageBox = $('#message'); 
      var $chat = $('#chat'); 

      $messageForm.submit(function(e){ 
       e.preventDefault(); 
       socket.emit('send message',$messageBox.val()); 
       $messageBox.val(""); 
      }); 
      socket.on('new message',function(data){ 
       $chat.append(data+ "<br/>"); 
      }); 
     }); 
    </script> 

答えて

3

thisのコンテキストは、あなたのコード内の問題です。現在のコンテキストを渡すには、bindまたはArrow関数を使用します。 JavaScriptで、thisの値は、によって定義されます。という関数は、socketというオブジェクトです。

socketEvents() 
{ 
    this.io.on('connection',(socket) => { 
     socket.on('send message',function(data) 
     { 
      this.io.emit('new message',data);//here the error lies. 
     }bind(this)); 
    }); 
} 

P.S .:このコードは正しく編集されました。私はそれについて下記の投稿を提案したいと思います。

What are the differences (if any) between ES6 arrow functions and functions bound with Function.prototype.bind?

+0

しかし、私は正確に似別のアプリケーションを持っている、違いは、私がerrorneous構文「this.io.emit」を使用している場合はエラーを与えないangularJSを使用していますがあります。どちらが私の心を爆破するか。両方(javascriptとangularJS)で "this"を扱うことに違いはありますか? – sagar

+0

そのコードを表示できますか?それはコードがどのように書かれているかに依存しますが、私が知る限り、これと同じように動作します。 – Agalo

+0

私は、実際の問題は矢の関数と関数を束縛することなく使用していることを知りました。 – sagar

0

このコンテキストは内部機能のために異なるからです。

試してみてください。

socketEvents() 
{ 
    this.io.on('connection',(socket) => { 
     socket.on('send message',function(data) 
     { 
      socket.emit('new message',data);//here the error lies. 
     }); 
    }); 
} 
+0

あなたは正しいですが、socket.onの中で "this"を使うための解決法を提供していないので、あなたの答えは意味をなさない。それは私の場合には役に立たない別の方法を提供するだけです。 "this"というキーワードを使う必要があります(必須)。 – sagar

関連する問題