GraphQl function not adding to subdocument array React

Viewed 40

I'm trying to make an ebay scraper using SerpApi, GraphQl and React. It displays all my needed data , saves and displays correctly from localstorage, but the actual GraphQl function returns null, maybe you guys can see my error (sorry for the info dump, just want to give as much info as possible)

    // create state for holding returned eBay api data
    const [searchedItems, setSearcheditems] = useState([]);

// Data collection function with references to other calculation functions
const searchData = () => ({
      itemName: organic_results[0]?.title,
      itemId: id,
      quantity: organic_results.length,
      itemImages: organic_results[0]?.thumbnail || [],
      price: parseFloat(averagePrice()),
      purchasePrice: parseFloat(searchInput.userPaid),
      percent: parseFloat(percentage()),
      profit: parseFloat(profit()),
    })

      setSearcheditems(searchData);

The handlSaveItem function connected to button click

  const handleSaveItem = async () => {

    const itemToSave = {...searchedItems};

    // get token
    const token = Auth.loggedIn() ? Auth.getToken() : null;

    if (!token) {
      return false;
    }

    try {
      await saveItem({
        variables: {item: itemToSave },
        
      });

      // if item successfully saves to user's account, save item to state

      setsavedItems([...savedItems, itemToSave]);
      console.log('item successfully saved', setsavedItems())

    } catch (err) {
      console.error(err);
    };
  };

The request payload looks okay

{operationName: "saveItem",…}
operationName: "saveItem"
query: "mutation saveItem($item: itemInput!) {\n  saveItem(item: $item) {\n    _id\n    username\n    email\n    savedItems {\n      itemId\n      purchasePrice\n      price\n      itemName\n      percent\n      profit\n      quantity\n      itemImages\n      purchasePrice\n      quantity\n      __typename\n    }\n    __typename\n  }\n}\n"
variables: {item: {itemName: "Hot Wheels Team Transport Nissan Skyline GT-R (BNR-34) Aero Lift JDM",…}}
item: {itemName: "Hot Wheels Team Transport Nissan Skyline GT-R (BNR-34) Aero Lift JDM",…}
itemId: "06dbdae7a5dcf13a1b2ffbf02fff2a08"
itemImages: "https://i.ebayimg.com/thumbs/images/g/0OoAAOSwWyhgbWTB/s-l225.jpg"
itemName: "Hot Wheels Team Transport Nissan Skyline GT-R (BNR-34) Aero Lift JDM"
percent: 214.1
price: 94.22
profit: 64.22
purchasePrice: 30
quantity: 21

But the response returns null for everything and returns multiple objects of null values.

{data: {saveItem: {_id: "61f4db7aa9d6bd0508130d93", username: "test", email: "test@test.com",…}}}
data: {saveItem: {_id: "61f4db7aa9d6bd0508130d93", username: "test", email: "test@test.com",…}}
saveItem: {_id: "61f4db7aa9d6bd0508130d93", username: "test", email: "test@test.com",…}
email: "test@test.com"
savedItems: [{itemId: null, purchasePrice: null, price: null, itemName: null, percent: null, profit: null,…},…]
0: {itemId: null, purchasePrice: null, price: null, itemName: null, percent: null, profit: null,…}
1: {itemId: null, purchasePrice: null, price: null, itemName: null, percent: null, profit: null,…}
2: {itemId: null, purchasePrice: null, price: null, itemName: null, percent: null, profit: null,…}
3: {itemId: null, purchasePrice: null, price: null, itemName: null, percent: null, profit: null,…}
4: {itemId: null, purchasePrice: null, price: null, itemName: null, percent: null, profit: null,…}
5: {itemId: null, purchasePrice: null, price: null, itemName: null, percent: null, profit: null,…}
6: {itemId: null, purchasePrice: null, price: null, itemName: null, percent: null, profit: null,…}
7: {itemId: null, purchasePrice: null, price: null, itemName: null, percent: null, profit: null,…}
8: {itemId: null, purchasePrice: null, price: null, itemName: null, percent: null, profit: null,…}
9: {itemId: null, purchasePrice: null, price: null, itemName: null, percent: null, profit: null,…}
10: {itemId: null, purchasePrice: null, price: null, itemName: null, percent: null, profit: null,…}
11: {itemId: null, purchasePrice: null, price: null, itemName: null, percent: null, profit: null,…}
12: {itemId: null, purchasePrice: null, price: null, itemName: null, percent: null, profit: null,…}
13: {itemId: null, purchasePrice: null, price: null, itemName: null, percent: null, profit: null,…}
14: {itemId: null, purchasePrice: null, price: null, itemName: null, percent: null, profit: null,…}
15: {itemId: null, purchasePrice: null, price: null, itemName: null, percent: null, profit: null,…}
16: {itemId: null, purchasePrice: null, price: null, itemName: null, percent: null, profit: null,…}
17: {itemId: null, purchasePrice: null, price: null, itemName: null, percent: null, profit: null,…}
18: {itemId: null, purchasePrice: null, price: null, itemName: null, percent: null, profit: null,…}
19: {itemId: null, purchasePrice: null, price: null, itemName: null, percent: null, profit: null,…}
20: {itemId: null, purchasePrice: null, price: null, itemName: null, percent: null, profit: null,…}
username: "test"
__typename: "User"
_id: "61f4db7aa9d6bd0508130d93"
0 Answers
Related