'I am not able to add data to MongoDB from Apollo Client

Below is the code to add data to my MongoDB Collection:

AddOrder.js

function AddOrder() {
  const d = new Date();
  let text = d.toString();
  const { currentUser } = useContext(AuthContext);
  console.log(currentUser)
  
  console.log(text)

  const { data } = useQuery(queries.GET_USER_BY_ID, {
    fetchPolicy: "cache-and-network",
    variables: {
        id: currentUser ? currentUser.uid : "none",
    },
});

let newCart = []
for(let i=0; i< data.getUser.cart.length; i++){
    newCart.push({quantity: data.getUser.cart[i].quantity, _id: data.getUser.cart[i]._id})
}
  const [addOrder] = useMutation(queries.ADD_ORDER); 

  addOrder({
    variables: {
        userId: currentUser.uid, status: 'ordered', createdAt: text, products: newCart
    }
  });
  
}

export default AddOrder;

This order is run when there is a successful payment made on stripe:

function Success() {
    console.log('Im here')
    return <div>
        <AddOrder /></div>;
}

export default Success;

Queries:

const GET_USER_BY_ID = gql`
    query ($id: String) {
        getUser(_id: $id) {
            _id
            name
            email
            address
            phoneNumber
            cart {
                _id
                name
                price
                quantity
            }
            createdAt
        }
    }
`;

const ADD_ORDER= gql`
    mutation Mutation($status: String!, $userId: String!, $createdAt: String, $products: [Pro]) {
        addOrder(status: $status, userId: $userId, createdAt: $createdAt, products: $products) {
            
            status
            userId
            createdAt
            products
            
        }
    }
`;

I am able to add orders using the localhost/4000 palyground but it throws an undefined getUser as data comes as empty. How can I fix this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source