javascript - 带有vue的Laravel不能做array.push
问题描述
我得到了一个产品组件,它有一个来自 2 个连接表products
和表的suppliers
表。当我添加数据时,我总是调用getProducts()
正在调用该index
方法的方法,并且我不希望每次添加产品或广播中的成功事件时都会发生这种情况。我试着this.products.push(e.product)
从我的广播活动中做。似乎我有一个错误Error in render: "TypeError: Cannot read property 'supplier_name' of undefined"
。有人能告诉我该怎么做吗?
这是我的产品控制器。索引和存储
public function index()
{
$products = Product::with('supplier')->get();
return response()->json($products);
}
public function store(Request $request)
{
$product = new Product;
$validator = \Validator::make($request->all(), [
'product_name' => 'required',
'barcode' => 'required|unique:products',
'price'=> 'required',
'category' => 'required',
'supplier' => 'required',
]);
if ($validator->fails()) {
//some validations
} else {
$product->barcode = $request->barcode;
$product->product_name = $request->product_name;
$product->price = $request->price;
$product->quantity = 0;
$product->category = $request->category;
$product->supplier_id = $request->supplier;
$product->save();
broadcast(new ProductsEvent(\Auth::user()->name, $product));
}
}
产品活动
class ProductsEvent implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $user, $product;
public function __construct($user, Product $product)
{
$this->user = $user;
$this->product = $product;
}
public function broadcastWith(): array
{
return [
'product' => $this->product,
'user' => $this->user
];
}
public function broadcastOn()
{
return new Channel('Products');
}
}
vue组件方法
addProduct(){
axios.post('/addProduct',{
product_name: this.product_name,
quantity: 0,
supplier: this.supplier.id,
category: this.category,
barcode: this.barcode,
price: this.price
}).then((res)=>{
this.clearValues()
$('#exampleModal').modal('hide')
toastr.success('Product Added!')
this.getProducts() //always calling this method
}).catch((res)=>{
toastr.error(res.message+' Check your Inputs')
})
},
created(){
this.getProducts()
this.getSuppliers()
window.Echo.channel('Products').listen('ProductsEvent',(e)=>{
//this.products.push(e.product) this one is the error
this.getProducts() // i need to call this everytime
console.log(e.product)
// console.log(e.product.product_name +' has been added by '+ e.user);
})
}
Console.log 输出
barcode: "2005"
category: "Phone"
created_at: "2020-05-08 15:35:28"
deleted_at: null
price: 500
product_id: 41
product_name: "Headset"
quantity: 0
supplier_id: 1
updated_at: "2020-05-08 15:35:28"
__proto__: Object
更新:将广播返回更改为
public function broadcastWith(): array
{
return [
'product' => $this->product->with('supplier')->orderBy('product_id', 'desc')->first(),
'user' => $this->user
];
}
从控制台登录时使用console.log(e.product)
{product_id: 57, barcode: "61", product_name: "Push 2", price: 200, quantity: 0, …}
barcode: "61"
category: "Phone"
created_at: "2020-05-08 17:33:14"
deleted_at: null
price: 200
product_id: 57
product_name: "Push 2"
quantity: 0
supplier: {supplier_id: 1, supplier_name: "Apple", supplier_address: "California", supplier_email: "apple@icloud.com", supplier_phone: "09270277397", …}
supplier_id: 1
updated_at: "2020-05-08 17:33:14"
__proto__: Object
似乎 DOM 没有更新使用this.products.push(e.product)
解决方案
推荐阅读
- pandas - pandas groupby 并表示某列是否有某个值
- scala - 在递归函数情况下,不执行 nil
- c# - C#中的日期时间格式用法
- javascript - 如何从 422 Unprocessable Entity 响应中获取错误消息?
- node.js - How to scrape website which is dynamically rendered with filters?
- r - renderDataTable 中单元格的条件格式
- javascript - Angular忽略证书签名
- tensorflow - 变分自动编码器无法使用小输入值进行训练
- laravel - 使用 Barryvdh Laravel 包在来自 html 的 pdf 的每一页上添加页眉页脚
- jquery - 使用 Jquery 随机插入带有超链接的图像