[英]How to update an item after being newly created in AWS DynamoDB and Amplify
I am trying to update a query in AWS Dynamo using AWS Amplify on top of Next.js.我正在尝试在 Next.js 之上使用 AWS Amplify 更新 AWS Dynamo 中的查询。
My scenario is simple.我的场景很简单。
On page load, if there exists a user and the user has not visited a page before, a new object will be created with set values using SWR.在页面加载时,如果存在用户并且该用户之前没有访问过页面,则将使用 SWR 使用设置值创建一个新的 object。
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)
Currently, this works.目前,这有效。 The object is added to the database with the above attributes. object 已添加到具有上述属性的数据库中。 HOWEVER, when I click a button to update this newly created object, I get an error of path: null, locations: (1) […], message: "Variable 'input' has coerced Null value for NonNull type 'ID!'"
但是,当我单击一个按钮来更新这个新创建的 object 时,我收到path: null, locations: (1) […], message: "Variable 'input' has coerced Null value for NonNull type 'ID!'"
This is my call to update the object when I click a button with the onClick handler "handleDBQuery".这是我在单击带有 onClick 处理程序“handleDBQuery”的按钮时更新 object 的调用。
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`)
}
My hunch is that the updateSite query does not know about the createSite query on page load.我的预感是 updateSite 查询不知道页面加载时的 createSite 查询。
In short, how can I update an item after I just created it?简而言之,我如何在刚刚创建项目后更新它?
I looked into the code at master branch and follow along as you describe.我查看了 master 分支的代码,并按照您的描述进行操作。 I found that the data?.id
here comes from a state variable and it is set only before the call to createSite
.我发现这里的data?.id
来自 state 变量,并且仅在调用createSite
之前设置。 I suggest you try setId again using the data returned from the createSite
我建议您使用从createSite
返回的数据再次尝试 setId
Try this尝试这个
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)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.