0

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>
  );
};```
MWO
  • 2,627
  • 2
  • 10
  • 25
Andres Pelaez
  • 77
  • 1
  • 1
  • 7
  • if you `console.error(err);` - then you can use the browsers developer tools console to inspect the logged error and get all the details of the error - assuming `alert(err)` is what you're talking about - it's not really clear which part of your code is doing that, but that's my best assumption – Bravo Feb 18 '22 at 03:17
  • Hi. Thank you for your answer. I try that and now I'm getting data: null. I don't know why. – Andres Pelaez Feb 18 '22 at 03:34

0 Answers0