Next.js -错误TypeError:无法重新定义属性:$$id

44u64gxh  于 2023-11-18  发布在  其他
关注(0)|答案(2)|浏览(89)

我正在编写一个Next.js教程,下面沿着一个YouTube视频。这是一个克隆的Facebook线程,我正在努力学习Next.js。对于创建一个线程的一部分a.k.a.(简单的帖子)。我从应用程序中得到这个500错误:

- error node_modules/next/dist/build/webpack/loaders/next-flight-loader/action-proxy.js (37:11) @ defineProperties
- error TypeError: Cannot redefine property: $$id
    at Function.defineProperties (<anonymous>)
    at eval (./lib/actions/thread.actions.ts:43:81)
    at (actionBrowser)/./lib/actions/thread.actions.ts (/Users/danielrubio/Desktop/threads/.next/server/app/(root)/create-thread/page.js:3366:1)
    at Function.__webpack_require__ (/Users/danielrubio/Desktop/threads/.next/server/webpack-runtime.js:33:42)
null

字符串
我可以从输出中看到罪魁祸首是thread.actions.ts的第43行。然而,我在那个文件中没有第43行。下面是完整的文件:

'use server'

import User from "../models/user.model";
import Thread from "../models/thread.model";
import { connectToDB } from "../mongoose";
import { revalidatePath } from "next/cache";

interface Params {
  text: string,
  author: string,
  communityId: string | null,
  path: string,
}

export async function createThread({text, author, communityId, path} :Params) {
  try {
    connectToDB();

    const createdThread = await Thread.create({
      text,
      author,
      community: null,
    })
  
    await User.findByIdAndUpdate(author, {
      $push: { threads: createdThread._id }
    })
  
    revalidatePath(path);
  } catch(error: any) {
    throw new Error(`Failed to create thread: ${error.message}`);
  }
 
}

export default createThread;


该文件只包含37行,所以我不知道是什么原因导致了这个错误。从以前使用React的工作来看,我认为这是某种类型的TypeScript错误,但我不确定。
下面是一个线程的模型:

import mongoose from 'mongoose';

const threadSchema = new mongoose.Schema({
  text: { type: String, required: true },
  author: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'User',
    required: true
  },
  community: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Community'
  },
  createdAt: {
    type: Date,
    default: Date.now
  },
  parentId: {
    type: String
  },
  children: [
    {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'Thread'
    }
  ]
  
})

const Thread = mongoose.models.Thread || mongoose.model('Thread', threadSchema);

export default Thread;


下面是我用来创建线程的Form:

'use client'

import { useForm } from 'react-hook-form';
import { zodResolver } from '@hookform/resolvers/zod'
import * as z from 'zod';
import { usePathname, useRouter } from 'next/navigation';
import { Button } from '../ui/button';
import {
  Form,
  FormControl,
  FormDescription,
  FormField,
  FormItem,
  FormLabel,
  FormMessage,
} from "@/components/ui/form"
import { Textarea } from "@/components/ui/textarea"

import { ThreadValidation } from '@/lib/validations/thread';
import createThread from '@/lib/actions/thread.actions';

interface Props {
  userId: string;
}

function PostThread({ userId }: Props ) {
  const router = useRouter();
  const pathname = usePathname();

  const form = useForm({
    resolver: zodResolver(ThreadValidation), 
    defaultValues: {
      threads: '',
      accountId: userId,
    }
  })
  
  const onSubmit = async (values: z.infer<typeof ThreadValidation>) => {
    await createThread({
      text: values.thread,
      author: userId,
      communityId: null,
      path: pathname

    })

    router.push('/')
  }

  return(
    <Form {...form}>
      <form
        className='flex flex-col justify-start gap-10'
        onSubmit={form.handleSubmit(onSubmit)}
      >
  
        <FormField
          control={form.control}
          name='thread'
          render={({ field }) => (
            <FormItem className='flex w-full flex-col gap-3'>
              <FormLabel className='text-base-semibold text-light-2'>
                Content
              </FormLabel>
              <FormControl className='no-focus border border-dark-4 bg-dark-3 text-light-1'>
                <Textarea
                  rows={15}
                  {...field}
                />
              </FormControl>
              <FormMessage />
            </FormItem>
          )}
        />
    
        <Button type='submit' className='bg-primary-500'>
          Post Thread
        </Button>
      </form>
    </Form>
  )
  
}

export default PostThread;


我对Next.js完全陌生,所以我有点难以理解这个错误的含义。如果有人能帮助我了解如何解决这个问题,我将不胜感激。

vjrehmav

vjrehmav1#

在文件/next/src/build/webpack/loaders/next-flight-loader/action-proxy.ts
在第48行,尝试添加:

}, {configurable: true, writable: false})

字符串

uurity8g

uurity8g2#

这是在github上讨论的here,它似乎是由于将函数和函数本身都导出为默认值而导致的
如果这是你的问题所在,

export async function createThread...

export default createThread...

字符串
需要改变以

async function createThread...

export default createThread...

相关问题