问题描述
我制作了一个自定义登录页面,使用 firebase 作为 authProvider。 我正在尝试使其工作,以便我登录,然后使用提供的 user.id 使用正确的 API 密钥从 WooCommerce 站点获取和更新数据。 如果我在数据提供者中预定义了用户 ID,一切正常,就像这样
const user = async () => {await authProvider.checkAuth()};
const vendor = user.lengt > 0 ? user.uid: 'reader';
let WooCommerce = new WooCommerceAPI(wcConfig[host][vendor]);
如果我直接使用user.uid,在我登录之前它是未定义的,并且根本不会显示登录年龄。现在我确实想知道为什么我们要在认证之前尝试使用 dataprovider 以及如何通过它? 登录后供应商(用于woocommerce api)也未更新。
WooCommerce dataProvider 是自制的,它确实返回 401,但我想知道错误处理中是否缺少某些内容。
实际代码如下:
export default {
getList: (resource,params) => {
const search = params.filter.q;
const { page,per_page } = params.pagination;
const query = {
page: JSON.stringify(page),per_page: JSON.stringify(per_page),search: JSON.stringify(search)
};
let url = `${resource}/?${stringify(query)}`
if (resource === 'system_status'){
url = resource;
}
return WooCommerce.get(url,params.data)
.then((response) => {
return {
data: response.data,total: parseInt(response.headers['x-wp-total']),pages: parseInt(response.headers['x-wp-totalpages'])
};
})
.catch((error) => {
// Invalid request,for 4xx and 5xx statuses
console.log("Response Status:",error.response.status);
console.log("Response Headers:",error.response.headers);
console.log("Response Data:",error.response.data);
return { data: error.response.data };
})
.finally(() => {
// Always executed.
});
},getone: (resource,params) => {
return WooCommerce.get(resource + '/' + params.id)
.then((response) => {
// Successful request
return {
data: response.data
};
})
.catch((error) => {
// Invalid request,update: (resource,params) => {
return WooCommerce.put(resource + '/' + params.id,params.data)
.then((response) => {
// Successful request
return {
data: response.data
};
})
.catch((error) => {
// Invalid request,create: (resource,params) => {
return WooCommerce.post(resource,params.data)
.then((response) => {
// Successful request
return {
data: response.data
};
})
.catch((error) => {
// Invalid request,error.response.data);
return { data: error.response.data };
})
.finally(() => {
// Always executed.
});
}
}
解决方法
显然必须处理配置错误。
let WooCommerce = () => {
try {
return new WooCommerceAPI(config[host][vendor]);
} catch (error) {
console.error(error)
}
}
本来可以更好,但至少现在可以用了。