试图从推送器以console.log
显示数据。我正在使用拉拉维尔 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 令牌和 身份验证:用户,其 id 如下所示:
<meta name="csrf-token" content="{{ csrf_token() }}">
<mete name="user-id" content="{{Auth::check() ? Auth::user()->id: ''}}"
我认为这里的拼写错误
将 METE 更改为元(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);
});
试试这个
确保您使用正确的 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)
};
这可以记录每个推送器事件。它将在开发时有所帮助