Insert problem in my db

Hello I don’t understand why I don’t see the data in my db, do you have any idea?

route.jsx

import { db } from "@/config/db";
import { Users } from "@/config/schema";
import { NextResponse } from "next/server";

export async function POST(req) {
    const { user } = await req.json();

    try {
        const userInfo = await db.select().from(Users)

            .where(eq(Users.email, user?.primaryEmailAdress.emailAdress))
        console.log("User", userInfo);

        if (userInfo?.length == 0) {
            const SaveResult = await db.insert(Users)
                .values({
                    name: user?.fullName,
                    email: user?.primaryEmailAddress.emailAddress,
                    imageUrl: user?.imageUrl,
                }).returning({ Users })

            return NextResponse.json({ 'result': SaveResult[0].Users })
        }
        return NextResponse.json({ 'result': userInfo[0] })
    }
    catch (e) {
        return NextResponse.json({ error: e })
    }

}

provider.js

"use client"
import { useUser } from '@clerk/nextjs'
import axios from 'axios';
import React, { useEffect } from 'react'

function Provider({ children }) {

    const { user } = useUser();

    useEffect(() => {
        user && VerifyUser();
    }, [user])

    const VerifyUser = async () => {
        const dataResult = await axios.post('/api/verify-user', {
            user: user
        });

        console.log(dataResult.data)
    }
    return (
        <div>
            {children}
        </div>
    )
}

Hi,

In your code, there appears to be a mismatch in the property name used for the email address field. Specifically:

In route.jsx:

user?.primaryEmailAdress.emailAdress

Here, primaryEmailAdress and emailAdress both seem to have typos. It should likely be primaryEmailAddress and emailAddress.

In provider.js:

user?.primaryEmailAddress.emailAddress

This uses the correct spelling.

2 Likes

I just corrected it and I still have no data in my db.

try {
        const userInfo = await db.select().from(Users)

            .where(eq(Users.email, user?.primaryEmailAddress.emailAddress))
        console.log("User", userInfo);

        if (userInfo?.length == 0) {
            const SaveResult = await db.insert(Users)
                .values({
                    name: user?.fullName,
                    email: user?.primaryEmailAddress.emailAddress,
                    imageUrl: user?.imageUrl,
                }).returning({ Users })

            return NextResponse.json({ 'result': SaveResult[0].Users })
        }
        return NextResponse.json({ 'result': userInfo[0] })
    }

OK, then it is time to do some debugging.

What does userInfo log to the console?

Try logging the values individually to verify they are correct and not undefined or null:

console.log({
  name: user?.fullName,
  email: user?.primaryEmailAddress?.emailAddress,
  imageUrl: user?.imageUrl,
});

What does that give you?

1 Like

Also confirm your schema correctly handles the default values for id and credits, as your result seems to not define all values for a row. (What kind of a db is this?)

1 Like

Thanks to you it works I forgot an import.

1 Like