Search code examples
typescriptsupabase

Select inserted column Supabase


I am trying to select the row I have just inserted into Supabase and then use the values to call another functions but I am getting and error of Property 'OrderID' does not exist on type '{ [x: string]: any; }[]'. I get this error in OrderID and the Date column. I know that the data will return those values but how can I get rid of this error. This is how I'm doing it:

const pushToSupabase = async()=>{
        try{
            const { data,error } = await supabase
            .from('Orders')
            .insert({userID:userInfo.id,Total:totalPrice()}).select();
            if(error) throw error;
            else{
                console.log('added to orders');
                console.log(data);
                const OrderNo:string = data.OrderID;
                const Date:string = (data.Date).toString();
                AddToOrders(OrderNo,Date,totalPrice());
            }
        }catch(error){
            console.log(error);
        }
    }

Solution

  • I managed to fix it by looking at the docs and seeing what the data returns:

    {
      "data": [
        {
          ***COLUMN VALUES***
        }
      ],
      "status": 201,
      "statusText": "Created"
    }
    

    So I fixed it by doing this:

    const OrderNo:string = data[0].OrderID;
    const Date:string = (data[0].Date).toString();