【问题标题】:Not showing data in console.log from pusher不显示来自 pusher 的 console.log 中的数据
【发布时间】:2019-03-25 05:43:34
【问题描述】:

试图在 pusherconsole.log 中显示数据。我正在使用 laravel 5.8

app.js 文件有以下代码:

let userId = document.head.querySelector('meta[name="user-id"]').content;
Echo.private('App.User.' + userId)
    .notification((notification) => {
        console.log(notification.type);
    });

错误如下:

未捕获的类型错误:无法读取 null 的属性“内容”

我还在刀片中添加了 CFRS 令牌和 Auth:User,ID 如下:

<meta name="csrf-token" content="{{ csrf_token() }}">
    <mete name="user-id" content="{{Auth::check() ? Auth::user()->id: ''}}"

【问题讨论】:

  • 确保您从后端收到 200 个状态码。你确定吗?你可以从后端添加你的回复吗?您可以查看网络标签
  • 是的,我在Network 标签中获得了 200 个状态。 @bhavinjr
  • 你为什么删除答案? @bhavinjr
  • 您可以查看更新的帖子

标签: jquery laravel pusher laravel-5.8


【解决方案1】:

我认为这里的拼写错误

mete 更改为 meta (tag name is a not a proper)

 <meta name="user-id" content="{{Auth::check() ? Auth::user()->id: ''}}" />

无头使用

let userId = document.querySelector('meta[name="user-id"]').content;
console.log("user Id"+userId);
Echo.private('App.User.' + userId)
    .notification((notification) => {
       console.log(notification.type);
});

【讨论】:

  • 我收到此错误:app.js:38378 POST http://localhost/broadcasting/auth 404 (Not Found)
  • @KINNARI 告诉你broadcasting/auth api 找不到所以添加或检查你的web.php
  • 实际上,我没有创建任何broadcasting,但我正在尝试从 Pusher 获取数据。我创建了 php artisan make:notification 。
【解决方案2】:

试试这个

确保您使用正确的 ID

let userId = document.querySelector('meta[name="user-id"]').content;
Echo.private('users.' + userId)
    .notification((notification) => {
        console.log('received');
        console.log(notification);
    });

确保你有相同的频道路线

Broadcast::channel('users.{id}', function ($user, $id) {
    return (int) $user->id === (int) $id; // it should be true (return true)
});

建议

您可以使用推送器启用全局控制台日志

Pusher.log = function(message) {
    window.console.log(message)
};

这可以记录每个推送事件。这将有助于开发

【讨论】:

  • 其实我并没有创建广播频道。我不知道如何创建它。我只是想从 pusher 获取数据到 console.log。
  • 你可以去这个教程pusher.com/tutorials/chat-laravel
  • 其实,我只是想用 laravel echo 和 pusher 发出通知。如果您不介意,我们可以继续聊天。 @bhavinjr
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-04-12
  • 1970-01-01
  • 2018-11-27
  • 2017-07-19
  • 2021-08-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多