I'm making an app in which I want to save data in Dynamodb from AWS. The very first time it work. After I delete the elements I had in Dynamodb, I'm getting the error [object Object]. I don't know what else do to. Besides that, I update the mutation files to add two more elements. When I tried with the original code that work at the beginning, it does not work. Thank you and sorry to bother you!
import React, { useEffect, useState } from "react";
import Amplify, { API, graphqlOperation } from "aws-amplify";
import { createTodo } from "./graphql/mutations";
import { listTodos } from "./graphql/queries";
import awsExports from "./aws-exports";
Amplify.configure(awsExports);
const initialState = { name: "", description: "", username: "", lastname: "" };
const App = () => {
const [formState, setFormState] = useState(initialState);
const [todos, setTodos] = useState([]);
useEffect(() => {
fetchTodos();
}, []);
function setInput(key, value) {
setFormState({ ...formState, [key]: value });
}
async function addTodo() {
try {
if (!formState.name || !formState.description) return;
const todo = { ...formState };
setTodos([...todos, todo]);
setFormState(initialState);
await API.graphql(graphqlOperation(createTodo, { input: todo }));
} catch (err) {
alert(err);
}
}
return (
<div style={styles.container}>
<h2>Amplify Todos</h2>
<input
onChange={(event) => setInput("name", event.target.value)}
style={styles.input}
value={formState.name}
placeholder="Name"
/>
<input
onChange={(event) => setInput("description", event.target.value)}
style={styles.input}
value={formState.description}
placeholder="Description"
/>
<input
onChange={(event) => setInput("username", event.target.value)}
style={styles.input}
value={formState.username}
placeholder="Username"
/>
<input
onChange={(event) => setInput("lastname", event.target.value)}
style={styles.input}
value={formState.lastname}
placeholder="Lastname"
/>
<button style={styles.button} onClick={addTodo}>
Create Todo
</button>
</div>
);
};```