adrianhajdin / project_next_14_ai_prompt_sharing

Next.js recently became the official React framework as outlined in React docs. In this course, you'll learn the most important Next.js concepts and how they fit into the React ecosystem. Finally, you'll put your skills to the test by building a modern full-stack Next 14 application.
https://www.jsmastery.pro/ultimate-next-course
2.83k stars 416 forks source link

r is not a function。When attempting to use the search box function through mongoose requests. #86

Open cxp-13 opened 9 months ago

cxp-13 commented 9 months ago

The information about error above:

TypeError: r is not a function
    at E:\nextjs_project\promptopia\promptopia\node_modules\next\dist\compiled\next-server\app-route.runtime.dev.js:6:63257
    at E:\nextjs_project\promptopia\promptopia\node_modules\next\dist\server\lib\trace\tracer.js:133:36
    at NoopContextManager.with (E:\nextjs_project\promptopia\promptopia\node_modules\next\dist\compiled\@opentelemetry\api\index.js:1:7062)     
    at ContextAPI.with (E:\nextjs_project\promptopia\promptopia\node_modules\next\dist\compiled\@opentelemetry\api\index.js:1:518)
    at NoopTracer.startActiveSpan (E:\nextjs_project\promptopia\promptopia\node_modules\next\dist\compiled\@opentelemetry\api\index.js:1:18093) 
    at ProxyTracer.startActiveSpan (E:\nextjs_project\promptopia\promptopia\node_modules\next\dist\compiled\@opentelemetry\api\index.js:1:18854)
    at E:\nextjs_project\promptopia\promptopia\node_modules\next\dist\server\lib\trace\tracer.js:122:103

my api content about search box function:

'use client'
import Prompt from "@models/prompt";
import { connectToDB } from "@utils/database";
import { connect, Document } from "mongoose";
import { useSearchParams } from "next/navigation";
import { NextResponse } from 'next/server'
// 搜索框功能,查询包含searchText的prompt,用户名,tag
export const GET = async (request) => {
    try {
        const searchText = useSearchParams().get("searchText");
        console.log("搜索框 searchText", searchText);

        await connectToDB()

        // 构建查询条件
        const query = {
            $or: [
                { prompt: { $regex: searchText, $options: 'i' } },
                { 'creator.username': { $regex: searchText, $options: 'i' } },
                { tag: { $regex: searchText, $options: 'i' } },
            ],
        };

        // 执行查询
        const results = await Prompt.find(query).exec();
        console.log("搜索框 result", results);

        return NextResponse.json(JSON.stringify(results), {
            status: 200
        })

    } catch (error) {
        console.log("搜索框 error", error);
        return new Response(JSON.stringify("Failed to search prompts"), {
            status: 500
        })
    }
}