【问题标题】:How to update an item after being newly created in AWS DynamoDB and Amplify如何在 AWS DynamoDB 和 Amplify 中新建项目后更新项目
【发布时间】:2022-01-19 13:14:16
【问题描述】:

我正在尝试在 Next.js 之上使用 AWS Amplify 更新 AWS Dynamo 中的查询。

我的场景很简单。

在页面加载时,如果存在用户并且该用户之前没有访问过页面,则将使用 SWR 创建一个具有设置值的新对象。

const fetchUserSite = async (owner, code) => {
  try {
    // Create site object if no site exists
    if (userData == null) {
      const siteInfo = {
        id: uuidv4(),
        code: parkCode,
        owner: user?.username,
        bookmarked: false,
        visited: false,
      }

      await API.graphql({
        query: createSite,
        variables: {input: siteInfo},
        authMode: 'AMAZON_COGNITO_USER_POOLS',
      })

      console.log(`${code} added for the first time`)
    }
    return userData || null
  } catch (err) {
    console.log('Site not added by user', data, err)
  }
}


 // Only call the fetchUserSite method if `user` exists
  const {data} = useSWR(user ? [user?.username, parkCode] : null, fetchUserSite)

目前,这有效。对象被添加到具有上述属性的数据库中。但是,当我单击一个按钮来更新这个新创建的对象时,我收到一个错误 path: null, locations: (1) […], message: "Variable 'input' has coerced Null value for NonNull type 'ID!'"

这是我在单击带有 onClick 处理程序“handleDBQuery”的按钮时更新对象的调用。

const handleDBQuery = async () => {
  await API.graphql({
    query: updateSite,
    variables: {
      input: {
        id: data?.id,
        bookmarked: true,
        owner: user?.username,
      },
    },
    authMode: 'AMAZON_COGNITO_USER_POOLS',
  })
  console.log(`${name} Bookmarked`)
}

我的直觉是 updateSite 查询不知道页面加载时的 createSite 查询。

简而言之,我如何在创建项目后更新它?

【问题讨论】:

  • 也许data?.id 恰好在这里未定义?云你 console.log(data?.id) 在 updateSite 的 await API.graphql({ 行上方。
  • data?.id 未定义。有没有更好的方法来实现这个功能?
  • @asinkxcoswt 我遇到了一些无法解释的行为。当我似乎单击另一个窗口或切换选项卡并再次单击按钮以更新站点时,数据会更新。为什么会这样?
  • 在这种情况下,我认为问题不在于 graphql 或 Amplify 本身,而可能与您在 React 中管理状态的方式有关。要回答这个问题,我需要更详细的源代码版本。也许,你能把它上传到 Github 并给我一个链接吗?
  • 嗨 @asinkxcoswt,这是我的 GitHub 存储库:github.com/tyler-morales/visit-national-parks/blob/bug/…

标签: amazon-web-services graphql next.js aws-amplify swr


【解决方案1】:

我查看了 master 分支的代码,并按照您的描述进行操作。我发现这里的data?.id 来自一个状态变量,它只在调用createSite 之前设置。建议您使用createSite返回的数据再次尝试setId

试试这个

const fetchUserSite = async (owner, code) => {
  try {
    // Create site object if no site exists
    if (userData == null) {
      const siteInfo = {
        id: uuidv4(),
        code: parkCode,
        owner: user?.username,
        bookmarked: false,
        visited: false,
      }

      const { data: newData } = await API.graphql({
        query: createSite,
        variables: {input: siteInfo},
        authMode: 'AMAZON_COGNITO_USER_POOLS',
      });

      setId(newData.id); // <====== here (or setId(siteInfo.id))

      console.log(`${code} added for the first time`)
      return newData; // <======= and this, maybe? (you may have to modify the qraphql query to make it return the same item as in the listSite
    }
    return userData || null
  } catch (err) {
    console.log('Site not added by user', data, err)
  }
}

【讨论】:

  • 感谢您查看此内容。这很完美。正在发生的事情,至少我认为正在发生的事情是,当我尝试从 updateSite 查询中调用 data?.id 时,它还没有被全局定义,所以它无法访问 id。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-28
  • 2019-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多