Well, whenever the component is created, the booking property in the component's datais null. How is that booking intended to get a value (object) that would have an id?
Jan 20, 2020
57
Level 6
TypeError: null is not an object
I am currently trying to listen to a store method which will trigger a BookingEvent to broadcast that a simple booking has been created, which will then push the Booking to a loop within my component. I am getting all Bookings through a Resource, which will list the current bookings.
View:
<booking :bookings="{{ $bookings->toJson() }}" />
Component:
<template>
<div>
<div v-for="booking in bookings">
<div>
<p>{{booking.user.name}}</p>
</div>
</div>
</div>
</template>
<script>
export default {
props: ['bookings'],
data() {
return {
booking: null, // error
}
},
methods:{
getNotes() {
axios.post("/getBookings").then(res => {
this.booking = res.data.data;
});
},
pushToBookings(booking) {
this.bookings.push({
booking: booking,
});
},
},
created() {
this.getBookings();
window.Echo.private(`Bookings.${this.booking.id}`)
.listen("BookingEvent", (e) => {
this.booking = e.booking;
this.pushToBookings(this.booking);
});
},
};
</script>
BookingEvent
class BookingEvent
{
public $booking;
public function __construct(Booking $booking)
{
$this->booking = $booking;
}
public function broadcastOn()
{
return new PrivateChannel("Bookings.{$this->booking->id}");
}
}
Controller
public function store(Booking $booking, Request $request)
{
$user = Auth::user();
$booking = $user->booking()->create([...]);
broadcast(new BookingEvent($booking));
return response($user->id, 200);
}
The loop is yet not User related, just a real-time data aggregation on the booking model, and I would like to push the new Booking to the loop whenever the Event is triggered.
Currently I get an error of "TypeError: null is not an object (evaluating 'this.booking.id')".
Please or to participate in this conversation.