未捕获的TypeError:在使用laravel实现pusher时,回调不是一个函数

时间:2017-04-04 22:24:42

标签: javascript laravel vuejs2 pusher

我正在制作一个简单的聊天应用程序,在连接到推送器时出现此错误,我使用laravel 5.4

Uncaught TypeError: callback is not a function
    at app.js:15350
    at PresenceChannel.Dispatcher.emit (app.js:34417)
    at PresenceChannel.handleEvent (app.js:35936)
    at app.js:33001
    at ConnectionManager.Dispatcher.emit (app.js:34417)
    at message (app.js:36336)
    at Connection.Dispatcher.emit (app.js:34417)
    at message (app.js:35789)
    at TransportConnection.Dispatcher.emit (app.js:34417)
    at TransportConnection.onMessage (app.js:34320)

我已将我的应用数据放入正确的文件和密钥中,我只是无法弄清楚发生了什么,当我删除Echo功能几行时它没有显示错误,你能帮助我吗&# 39;那个错误是关于的,这是我的前端文件App.js

/**
 * First we will load all of this project's JavaScript dependencies which
 * includes Vue and other libraries. It is a great starting point when
 * building robust, powerful web applications using Vue and Laravel.
 */

require('./bootstrap');

/**
 * Next, we will create a fresh Vue application instance and attach it to
 * the page. Then, you may begin adding components to this application
 * or customize the JavaScript scaffolding to fit your unique needs.
 */

Vue.component('example', require('./components/Example.vue'));
Vue.component('chat-message', require('./components/ChatMessage.vue'));
Vue.component('chat-log', require('./components/ChatLog.vue'));
Vue.component('chat-composer', require('./components/ChatComposer.vue'));

const app = new Vue({
    el: '#app',
    data: {
        messages: []   
            },
    methods: {
        addMessage(message){
            // add to existing messages
            this.messages.push(message);

            // persist to the database
            axios.post('/messages', message).then(response=>{
                // do whatever
            });
        }
    },
    created(){
        // axios uses promises so we could do .then
        axios.get('/messages').then(response=>{
            this.messages = response.data;
        });

        Echo.join('chatroom')
            .here()
            .joining()
            .leaving()
            .listen('MessagePosted', (e)=>{
                console.log(e);
            });

    }
});

这是我的MessagePosted创建的事件

<?php

namespace App\Events;
use App\Message;
use App\User;

use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

class MessagePosted implements ShouldBroadcast
{

    public $message = new Message;


    public $user = new User;

    use Dispatchable, InteractsWithSockets, SerializesModels;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct(Message $message, User $user)
    {
        $this->message = $message;
        $this->user = $user;
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return Channel|array
     */
    public function broadcastOn()
    {
        return new PresenceChannel('chatroom');
    }
}

1 个答案:

答案 0 :(得分:2)

方法herejoiningleaving将回调作为参数。 对参数进行类型检查以验证它是一个函数,回调是未定义的,因为没有参数,你得到了  Uncaught TypeError: callback is not a function

Echo.join('chatroom')
    .here()
    .joining()
    .leaving()
    .listen('MessagePosted', (e)=>{
        console.log(e);
    });