Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7082699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:05:12+00:00 2026-05-28T07:05:12+00:00

io.sockets.on(‘connection’, function (socket) { socket.on(‘requestGame’, function (data) { for (var game in games) if

  • 0
io.sockets.on('connection', function (socket) {                
    socket.on('requestGame', function (data) {

    for (var game in games)
        if (game.player2 === undefined) {
            game.player2 = socket;
            socket.emit('gameStart', { game_id: game.game_id, turn: !p1_turn }); // works
            game.player2.emit('gameStart', { game_id: game.game_id, turn: !p1_turn }); // doesn't work

Why does one of these lines work while the other doesn’t?

here is the error

game.player2.emit('gameStart', { game_id: game.game_id, turn: !game.p1_tur
             ^
TypeError: Cannot call method 'emit' of undefined
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T07:05:13+00:00Added an answer on May 28, 2026 at 7:05 am

    The for (var a in b) syntax iterates through b’s keys. Each time through the loop, a will be a string, and not the value of b that you were probably looking for.

    Because it’s a string and a literal, attaching a property to it will immediately not have any effect on it. You cannot change literals like strings and numbers in Javascript.

    'hello'.foo = 'world';
    console.log('hello'.foo); // undefined
    var str = 'hello';
    str.foo = 'world';
    console.log(str.foo); // undefined
    

    What you probably meant to do was

        for (var key in games)
            var game = games[key];
            if (game.player2 === undefined) {
                game.player2 = socket;
                socket.emit('gameStart', { game_id: game.game_id, turn: !p1_turn }); // works
                game.player2.emit('gameStart', { game_id: game.game_id, turn: !p1_turn }); // doesn't work
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am using raw sockets to create my own socket. i need to set
There are two connected sockets. How can I interconnect them? Data appeared on the
Using flash sockets in my irc client, can I serve the socket policy file
in C# when a sockets connection is terminated the other node is informed of
Using sockets in C, is it safe to do this in a function? int
Blocking berkeley sockets normally used by Ruby has one downside: if connection with remote
I'm working with asyn sockets now. I have a callback function 'onDataReceived', which is
I'm sending data from client sockets through a proxy server to an http server.
I plan to use sockets to make function calls between a client 32 bit
I was playing with sockets on local machine with no network connection. See below:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.