如何从放大数据存储中删除对象

发布于 2025-02-03 19:50:42 字数 1288 浏览 3 评论 0原文

我设法使用以下方式从前端删除对象:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文