Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot read property 'on' of undefined #1

Open
marcoippolito opened this issue Jan 10, 2020 · 5 comments
Open

Cannot read property 'on' of undefined #1

marcoippolito opened this issue Jan 10, 2020 · 5 comments

Comments

@marcoippolito
Copy link

Hi!
in order to fully understand how to use peerjs with vuejs, I'm trying to replicate what you excellently you have done but without pug.

This my very easy Peerjs.vue:

<template>
  <div>
    <p>My Peer Id : {{ myPeerId }}</p>
    <p>Remote Peer Id
      <input v-model="remotePeerId">
    </p>
    <p button @click="connect()">Connect to my friend!</p>
    <form>
      <label>Message To Send:</label>
      <input v-model="message" type="text">
    </form>
    <p button @click="sendMessage()">Send Message!</p>
    <hr>
    <div class="col-xs-12 col-sm-6">
      <ul class="list-group">
        <li class="list-group-item" v-for="message in messages">Message: {{ message }}</li>
      </ul>
    </div> 
  </div>
</template>

<script>
    import Peer from 'peerjs';

    export default {
      data: () => ({
        connection: null,
        connecting: true,
        message: '',
        messages: [],
        myPeer: null,
        myPeerId: '',
        remotePeerId: ''
      }),
      created() {
        this.myPeerId = (Math.random() * 6 + Math.random()).toString(36).replace('.', '');
        this.myPeer = new Peer({ key: 'lwjd5qra8257b9' })
        this.myPeer.on('open', id => {
          console.log('Connected at PeerJS server with success')
          this.myPeerId = id;
          this.connecting = false
        })
        this.myPeer.on('connection', con => {
          this.connection = con
          con.on('open', () => {
            con.on('data', newMessage => this.messages.push(newMessage))
          })
        })
      },
      computed: {
      },
      methods: {
        connect() {
          if (this.remotePeerId.trim().length) {
            this.connection = this.myPeer.connect(this.remotePeerId)
            this.connection.on('open', () => {
              this.connection.on('data', newMessage => this.messages.push(newMessage))
            })
        }
      },
      sendMessage() {
        const newMessage = {
          id: this.myPeerId,
          message: this.message
        }
        if (this.connection) {
          this.connection.send(newMessage)
        }
        this.messages.push(newMessage)
        this.message = ''
      }
    },
    filters: {
    }
  }
</script>

When clicking to "Connect to my friend!" I get "Error in v-on handler: "TypeError: Cannot read property 'on' of undefined"

image

What's wrong with

    this.connection.on('open', () => {
      this.connection.on('data', newMessage => this.messages.push(newMessage))
    })

?

Looking forward to your kind help.
Marco

@mahenrique94
Copy link
Owner

Hi Marco, you code looks correct, can you share your repo with me? I'll to try simulate this problem here locally.

Tip: Add a console.log inside this.myPeer.on('connection' at the begin, before this.connection assignment:

this.myPeer.on('connection', con => {
    this.connection = con
    con.on('open', () => {
        con.on('data', newMessage => this.messages.push(newMessage))
    })
})

It must log a connection object.

@marcoippolito
Copy link
Author

Hi!
I put everything here: https://github.com/marcoippolito/PeerJS-VueJS-Test

@mahenrique94
Copy link
Owner

Hi Marco, the problem it's peerjs version.

You're using a recent version, something need to be done of the new way.

Att, Matheus

@marcoippolito
Copy link
Author

marcoippolito commented Jan 15, 2020

Hi Matheus,

after few trials, now it is working https://github.com/marcoippolito/PeerJS-VueJS-Test

@mahenrique94
Copy link
Owner

Hey Marco, nice man.

Well done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants