如何从放大数据存储中删除对象
我设法使用以下方式从前端删除对象:
const deleteItem = async (index) => {
let removeItem = [...storeItems];
removeItem.splice(index, 1);
setStoreItems(removeItem);
};
现在我试图从Amplify Datastore查询的列表中删除对象,并将其设置为这样的状态“ StoreItems”:
const deleteItem = async (index) => {
let removeItem = [...storeItems];
removeItem.splice(index, 1);
setStoreItems(removeItem);
const todelete = await DataStore.query(StoreItem, storeItems.id);
DataStore.delete(todelete);
};
我一直遇到此错误:
[Unhandled promise rejection: Error: Object is not an instance of a valid model]
列表为在使用效果中查询:
useEffect(() => {
// Fetch Comments
const fetchStoreItem = async () => {
const userInfo = await Auth.currentAuthenticatedUser();
const userSub = userInfo.attributes.sub;
const user = (await DataStore.query(User)).find(u => u.sub === userSub);
if (!storeItems) {
return;
}
const storeItem = (await DataStore.query(StoreItem))
.filter((StoreItem) => StoreItem.userID === user.id);
setStoreItems(storeItem)
};
fetchStoreItem();
}, [storeItems]);
我该怎么做?
I manage to delete an object from the front end using this:
const deleteItem = async (index) => {
let removeItem = [...storeItems];
removeItem.splice(index, 1);
setStoreItems(removeItem);
};
Now i'm trying to delete the object from a list queried from amplify datastore and set to a state "storeItems" like this:
const deleteItem = async (index) => {
let removeItem = [...storeItems];
removeItem.splice(index, 1);
setStoreItems(removeItem);
const todelete = await DataStore.query(StoreItem, storeItems.id);
DataStore.delete(todelete);
};
And i keep getting this error:
[Unhandled promise rejection: Error: Object is not an instance of a valid model]
The list is queried in a useEffect:
useEffect(() => {
// Fetch Comments
const fetchStoreItem = async () => {
const userInfo = await Auth.currentAuthenticatedUser();
const userSub = userInfo.attributes.sub;
const user = (await DataStore.query(User)).find(u => u.sub === userSub);
if (!storeItems) {
return;
}
const storeItem = (await DataStore.query(StoreItem))
.filter((StoreItem) => StoreItem.userID === user.id);
setStoreItems(storeItem)
};
fetchStoreItem();
}, [storeItems]);
How can i go about doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论