试图在hilla(vaadin)中创建一个应用程序,该应用程序将列出产品及其详细信息。要对产品进行分类,需要存储与类别相关的信息,但要在hilla/vaadin网格中列出该类别时所面临的问题。从端点返回一个类别列表,并在存储中使用该列表,并尝试在网格中显示。当尝试运行该应用程序时,将该错误作为“
Type 'Promise<void>' is missing the following properties from type“
@Nonnull是在列表的响应中指定的,方法中仍然存在错误
下面是端点类
@Endpoint
@AnonymousAllowed
public class ProductEndpoint {
private InterProductService productService;
@Autowired
public ProductEndpoint(InterProductService productService) {
this.productService = productService;
}
public List<ProductDataList> fetchAllProducts() {
return this.productService.fetchProducts("");
}
public @Nonnull List<@Nonnull ProductCategoryDataList> fetchAllProdCategory() {
return this.productService.fetchProductCategory("");
}
@Nonnull
public EndpointResponse saveCatgeory(@Nonnull CategoryDetails categoryDetails) {
return this.productService.saveCategory(categoryDetails);
}
}下面是商店
export class ProductStore {
constructor() {
makeAutoObservable(
this);
this.fetchProductCatgeory();
}
async saveProductCategory(prodCategory: CategoryDetails) {
const responseData = await ProductEndpoint.saveCatgeory(prodCategory);
return responseData;
}
async fetchProductCatgeory() {
const prodCatData = await ProductEndpoint.fetchAllProdCategory();
runInAction(() => {
return prodCatData;
});
}
}特定于该产品模块和catgeory的存储类
export class CategoryListRegisterViewStore {
categoryList: ProductCategoryDataList[] = [];
constructor() {
makeAutoObservable(
this,
{
categoryList: observable.shallow,
}
);
this.loadProductCategory();
}
loadProductCategory() {
//return appStore.tricampCrmProductStore.fetchProductCatgeory();
const prodCategory = appStore.tricampCrmProductStore.fetchProductCatgeory();
runInAction(() => {
this.categoryList = prodCategory;
});
}
saveProductCategory(categoryDetails: CategoryDetails) {
return appStore.tricampCrmProductStore.saveProductCategory(categoryDetails);
}
}
export const categoryListRegisterViewStore = new CategoryListRegisterViewStore();带有网格代码的Html页面如下所示
<vaadin-grid-column header="Action" frozen-to-end auto-width flex-grow="0" ${columnBodyRenderer(this.actionRenderer,
[])}>
</vaadin-grid-column>尝试过多种方法,如直接从方法返回,但获得不同的错误,如AllItem,是不迭代的。在检查承诺的同时,也出现了“不明确的”。所以也许我做错了,希望有人能支持我来解决这个问题
发布于 2022-11-22 19:09:19
代码中存在多个问题:
items属性绑定到方法以加载类别。应该将其绑定到从该方法加载的类别,这些类别应该存储在categoryListRegisterViewStore.categoryList中。
<vaadin-grid theme="row-stripes" .items="${categoryListRegisterViewStore.categoryList}" > CategoryListRegisterViewStore.loadProductCategory方法尝试将返回Promise的fetchProductCatgeory的结果分配给categoryList属性,该属性的类型为ProductCategoryDataList[]。这就是你得到类型错误的地方。您应该等待承诺解决,然后存储承诺的结果: async loadProductCategory() {
const prodCategory = await appStore.tricampCrmProductStore.fetchProductCatgeory();
runInAction(() => {
this.categoryList = prodCategory;
});
}fetchProductCatgeory方法存在缺陷。它不返回端点调用的结果,因为您将返回包装为一个runInAction。相反,只需直接从端点调用返回承诺: fetchProductCatgeory() {
return ProductEndpoint.fetchAllProdCategory();
}此时,您可以考虑完全直接使用loadProductCategory中的端点来删除该方法。
https://stackoverflow.com/questions/74537359
复制相似问题