Skip to content

406 Error without a Reason #2395

Closed
Closed

Description

@bfzli

Hello, devs, I'm using Supabase auth and Table to match the id with the Authentication then show data from the database, and it was working correctly but now, I can't seem to fix it, can you check the code and answer the solution.

import {useState, useEffect} from 'react';
import {supabase} from '../db/supabase';
import {Helmet} from "react-helmet";
import styled from 'styled-components';
import Header from '../components/Header';

export default function Home() {
    const [session, setSession] = useState(null)
    const [username, setUsername] = useState(null)
    const [bio, setBio] = useState(null)
    const [avatar, setAvatar] = useState(null)
    const [first_name, setFirstName] = useState(null);
    const [last_name, setLastName] = useState(null)

    useEffect(() => {
        setSession(supabase.auth.session())
        supabase.auth.onAuthStateChange((_event, session) => {
            setSession(session)
        })
    }, [])

    getProfile()

    async function getProfile() {
        try {
            const user = supabase.auth.user()
            let {data, error, status} = await supabase
                .from('Accounts')
                .select(`username, bio, avatar, first_name, last_name`)
                .eq('id', user.id)
                .single()

            if (error && status !== 406) {
                throw error
            }

            if (data) {
                setUsername(data.username)
                setBio(data.bio)
                setAvatar(data.avatar)
                setFirstName(data.first_name)
                setLastName(data.last_name)
            }
        } catch (error) {
            alert(error.message)
        }
    }

    return (
        <>
            <Helmet>
                <title>{session ? `Preferencat për ${session.user.id} ` : null}</title>
                <meta name="description" content="Cicërimat një vend për tu shprehur." />
            </Helmet>
            {session ?
                <>
                    <Header />
                    <Container>
                        <ProfileCover>
                            <ProfilePhoto src={session ? avatar : 'https://media.istockphoto.com/photos/smiling-man-outdoors-in-the-city-picture-id1179420343?k=6&m=1179420343&s=612x612&w=0&h=y7GrwxrbixTWvJfaeiu55rWXMGYr6oP583uzJJ4-Kis='}></ProfilePhoto>
                        </ProfileCover>
                        <ProfileDown>
                            <ProfileInfo>
                                <ProfileName>{first_name} {last_name}</ProfileName>
                                <ProfileUsername>@{username}</ProfileUsername>
                                <ProfileTabBreak />
                                <ProfileStats>
                                    <Following>
                                        <FollowingCount>214</FollowingCount>
                                        <FollowingName>Duke i Ndjekur</FollowingName>
                                    </Following>
                                    <Followers>
                                        <FollowersCount>2221</FollowersCount>
                                        <FollowersName>Ndjekësit</FollowersName>
                                    </Followers>
                                    <Follow>Follow</Follow>
                                </ProfileStats>
                                <ProfileTabTitle>Bio</ProfileTabTitle>
                                <ProfileTabBreak />
                                <ProfileBio>{bio}</ProfileBio>
                            </ProfileInfo>
                            <ProfilePosts>
                                <h1>Cicërimat</h1>
                                <Article>
                                    <ArticleTop>
                                        <Profile>
                                            <ProfileImg src="itwaspicvariable" />
                                            <InfoPost>
                                            </InfoPost>
                                        </Profile>
                                    </ArticleTop>
                                    <ArticleMiddle>
                                    </ArticleMiddle>
                                    <ArticleBottom>
                                        <Like>
                                            <LikeIcon width="21" height="20">
                                            </LikeIcon>
                                            Pëlqe
                                        </Like>
                                        <Save>
                                            Ruaj
                                        </Save>
                                        <Copy>
                                            <CopyIcon width="21" height="20">
                                            </CopyIcon>
                                            Kopjo
                                        </Copy>
                                    </ArticleBottom>
                                </Article>
                            </ProfilePosts>
                        </ProfileDown>
                        {/* <ContainerLeft>
                            <Configurations>
                                <Section>
                                    <Title>Preferencat</Title>
                                </Section>
                                <Section>
                                    <Info>
                                        <Name>Emri dhe Mbiemri</Name>
                                        <Description>Të dhëna që shfaqën në profilin tënd të Cicërima</Description>
                                    </Info>
                                    <Area>
                                        <Input type="text" placeholder="Emri"></Input>
                                        <Input type="text" placeholder="Mbiemri"></Input>
                                    </Area>
                                </Section>
                                <Section>
                                    <Info>
                                        <Name>Email Adresa</Name>
                                        <Description>Kjo eshte email adresa e lidhur me kete akount.</Description>
                                    </Info>
                                    <Area>
                                        <Input type="text" value={session.user.email} placeholder="Adresa Elektronike" disabled />
                                    </Area>
                                </Section>
                                <Section>
                                    <Info>
                                        <Name>Usernami</Name>
                                        <Description>Usernami qe munde te kyqeni me, ajo automatikisht perdort dhe per urln.</Description>
                                    </Info>
                                    <Area>
                                        <Input type="text" value={username} onnChange={(e) => setUsername(e.target.value)} placeholder="Username" />
                                    </Area>
                                </Section>
                                <Section>
                                    <Info>
                                        <Name>Webfaqja</Name>
                                        <Description>Usernami qe munde te kyqeni me, ajo automatikisht perdort dhe per urln.</Description>
                                    </Info>
                                    <Area>
                                        <Input type="url" value={bio} onChange={(e) => setBio(e.target.value)} placeholder="Biografia" />
                                    </Area>
                                </Section>
                                <Section>
                                    <Info>
                                        <Name>Bio</Name>
                                        <Description>Usernami qe munde te kyqeni me, ajo automatikisht perdort dhe per urln.</Description>
                                    </Info>
                                    <Area>
                                        <Input type="url" placeholder="bio" value={avatar || ''} onChange={(e) => setAvatar(e.target.value)} />
                                    </Area>
                                </Section>
                                <Section>
                                    <button onClick={() => updateProfile({username, bio, avatar})}>Update</button>
                                </Section>
                            </Configurations>
                        </ContainerLeft> */}
                    </Container>
                </>
                :
                null
            }
        </>
    )
}

Activity

  1. added
    bug
    on Jul 16, 2021
  2. inian commented on Jul 17, 2021

    @inian
    Member

    406 error happens when you do a .single but there are more than 1 row returned. Try removing that?

    let {data, error, status} = await supabase
                    .from('Accounts')
                    .select(`username, bio, avatar, first_name, last_name`)
                    .eq('id', user.id)
    

    Let me know if the issue persists after that @bfzli

  3. bfzli commented on Jul 17, 2021

    @bfzli
    ContributorAuthor

    @inian remove what? can you explain more, because I need the following selection?

  4. geromegrignon commented on Jul 17, 2021

    @geromegrignon
    Contributor

    @bfzli

    single()

    Retrieves only one row from the result. Result must be one row (e.g. using limit), otherwise this will result in an error.

    You are getting an error because there are multiple rows with .eq('id', user.id).
    So you can't use .single().

  5. bfzli commented on Jul 17, 2021

    @bfzli
    ContributorAuthor

    @geromegrignon I took this from boilerplate at Supabase with React, I only added some queries with select, I didn't touch it the .eq and single

  6. geromegrignon commented on Jul 17, 2021

    @geromegrignon
    Contributor

    Can you share the link to this boilerplate?

  7. bfzli commented on Jul 17, 2021

    @bfzli
    ContributorAuthor

    @geromegrignon in fact this is on guide to the React on the Supabase: https://supabase.io/docs/guides/with-react
    You can see at src/Account.js and check the code that is using : .eq('id', user.id) .single()

  8. geromegrignon commented on Jul 17, 2021

    @geromegrignon
    Contributor

    There is no such code as :

     let {data, error, status} = await supabase
                    .from('Accounts')
                    .select(`username, bio, avatar, first_name, last_name`)
                    .eq('id', user.id)
                    .single()

    in the src/Account.js file.

    This guide uses the SQL Quick Start 'User management starter' available in the SQL tab of a project on app.supabase.io.
    The only API table used in this guide is called 'profiles' (included in the src/Account.js).

    So there is no 'Accounts' API table you can receive info from :

     let {data, error, status} = await supabase
                    .from('Accounts')
                    .select(`username, bio, avatar, first_name, last_name`)
                    .eq('id', user.id)
                    .single()

    I only added some queries with select

    If you added an 'Accounts' API table, you not only added some select but .insert() too.
    At some point, you are inserting multiple rows for a unique user in the Accounts table (so single() doesn't work)

    You can test it by running a query in the SQL tab on app.supabase.io :

    SELECT * FROM accounts WHERE id = 'my-user-id'
  9. bfzli commented on Jul 17, 2021

    @bfzli
    ContributorAuthor

    @geromegrignon Yes i did some changes to the database, but still is the same logic from react docs on supabase, this is what I was trying to say, because the code wont match with it, but the logic yes.

  10. geromegrignon commented on Jul 17, 2021

    @geromegrignon
    Contributor

    Can you move your project to Github and share the repository created? So i can take a look at it.

  11. reopened this on Jul 18, 2021
  12. inian commented on Jul 18, 2021

    @inian
    Member

    Hey @bfzli, i meant in your code change this

     let {data, error, status} = await supabase
                    .from('Accounts')
                    .select(`username, bio, avatar, first_name, last_name`)
                    .eq('id', user.id)
                    .single()

    to this

     let {data, error, status} = await supabase
                    .from('Accounts')
                    .select(`username, bio, avatar, first_name, last_name`)
                    .eq('id', user.id)

    Data will be an array above, so you will need to handle that. but you shouldn't get the 406 error anymore

  13. bfzli commented on Jul 18, 2021

    @bfzli
    ContributorAuthor

    Thank you, learning supabase.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions