• 로그인
  • 장바구니에 상품이 없습니다.

home2 게시판 Next.js 게시판 aws s3 이미지 미리보기 업로드 오류

aws s3 이미지 미리보기 업로드 오류

2 글 보임 - 1 에서 2 까지 (총 2 중에서)
  • 글쓴이
  • #103606

    김수현
    참가자
    이미지 업로드 미리보기해서 업로드 하는 중인데요. 다음과 같은 오류가 발생합니다.
    로그인한 사용자만 글을 쓰게 하기 위해서 session을 사용했는데, 'use client'로 바꿔서 그런 거 같다는 생각이 드는데, 어떻게 해야 할까요?
    
    오류문구
    ======================
     Failed to compile
    ./node_modules/@mapbox/node-pre-gyp/lib/clean.js:8:0
    Module not found: Can't resolve 'fs'
    https://nextjs.org/docs/messages/module-not-found
    Import trace for requested module:
    ./node_modules/@mapbox/node-pre-gyp/lib/ sync ^\.\/.*$
    ./node_modules/@mapbox/node-pre-gyp/lib/node-pre-gyp.js
    ./node_modules/bcrypt/bcrypt.js
    ./pages/api/auth/[...nextauth].js
    ./app/write/page.js
    This error occurred during the build process and can only be dismissed by fixing the error.
    
    
    작성코드
    ===========================
    
    'use client'
    import { authOptions } from "@/pages/api/auth/[...nextauth]"
    import { connectDB } from "@/util/database"
    import { getServerSession } from "next-auth"
    import { useState } from "react"
    export default async function Write() {
        let session = await getServerSession(authOptions)
        console.log(session)
        if (session == null) {
            return <div>로그인하세요</div>
        } else {
            let [src, setSrc] = useState('')
            return (
                <div className="p-20">
                    <h4>글작성</h4>
                    <form action="/api/post/new" method="POST">
                        <input name="title" placeholder="글제목 입력" />
                        <input name="content" placeholder="글내용 입력" />
                        <button type="submit">전송</button>
                    </form>
                    <input type="file" accept="image/*" onChange={
                        async (e) => {
                            let file = e.target.files[0]
                            let filename = encodeURIComponent(file.name)
                            let res = await fetch('/api/post/image?file=' + filename)
                            res = await res.json()
                            //S3 업로드
                            const formData = new FormData()
                            Object.entries({ ...res.fields, file }).forEach(([key, value]) => {
                                formData.append(key, value)
                            })
                            let 업로드결과 = await fetch(res.url, {
                                method: 'POST',
                                body: formData,
                            })
                            console.log(업로드결과)
                            if (업로드결과.ok) {
                                setSrc(업로드결과.url + '/' + filename)
                            } else {
                                console.log('실패')
                            }
                        }
                    } />
                    < img src={src} />
                </div>
            )
        }
    }
    #103629

    codingapple
    키 마스터
    client component에선 useSession 쓰면 로그인정보 나옵니다
2 글 보임 - 1 에서 2 까지 (총 2 중에서)
  • 답변은 로그인 후 가능합니다.

About

현재 월 700명 신규수강중입니다.

  (09:00~20:00) 빠른 상담은 카톡 플러스친구 코딩애플 (링크)
  admin@codingapple.com
  이용약관
ⓒ Codingapple, 강의 예제, 영상 복제 금지
top

© Codingapple, All rights reserved. 슈퍼로켓 에듀케이션 / 서울특별시 강동구 고덕로 19길 30 / 사업자등록번호 : 212-26-14752 온라인 교육학원업 / 통신판매업신고번호 : 제 2017-서울강동-0002 호 / 개인정보관리자 : 박종흠