contents, parent 부분은 잘 출력이 되는데 getServerSession이 null 값이 나옵니다 그러니 이메일 출력도 안되네요... 어떻게 해야 할까요?
오류메세지는 Cannot read properties of null (reading 'user') 라고 나옵니다 그런데 이미 getServerSession을 session변수에 담아 출력했을 때부터 null이라고 뜨네요
api/comment.js
import { getServerSession } from "next-auth";
import { connectDB } from "@/util/database";
import { ObjectId } from "mongodb";
import { authOptions } from "next-auth";
export default async function handler(req, res) {
let session = await getServerSession(req, res, authOptions)
console.log(session) // 이부분이 null 값이 나옵니다.
console.log(req.body)
if (req.method === 'POST') {
req.body = JSON.parse(req.body)
let content = {
contents: req.body.comment,
author: session.user.email,
parent: new ObjectId(req._id),
}
let client = await connectDB;
const db = client.db('forum');
let result = await db.collection('comment').insertOne(content);
res.status(200).json('댓글 작성 완료');
}
}
detail/[number]/comment.js
'use client'
import { useState } from "react"
export default function Comment(props) {
let [comment, setComment] = useState('')
return (
<div>
<div>댓글목록</div>
<input onChange={(e)=>{setComment(e.target.value)}}/>
<button onClick={()=>{
fetch('/api/comment', {
method: 'POST',
body: JSON.stringify({comment: comment, _id: props._id})
})
}}>댓글전송</button>
</div>
)
}