6 글 보임 - 1 에서 6 까지 (총 6 중에서)
-
글쓴이글
-
2023년 12월 28일 19:07 #108117
최예준참가자⨯ ./node_modules/mongodb/lib/client-side-encryption/mongocryptd_manager.js:34:24 Module not found: Can't resolve 'child_process'
https://nextjs.org/docs/messages/module-not-found
Import trace for requested module: ./node_modules/mongodb/lib/client-side-encryption/auto_encrypter.js ./node_modules/mongodb/lib/index.js ./util/DataBase.js ./src/pages/index.js
import {connectDB} from "../../util/DataBase";
// index.js export default async function Home() {
let client = await connectDB; const db = client.db('forum'); let result = await db.collection('post').find().toArray();
return ( <main> {result[0].title} </main> ) }
// database.js
import { MongoClient } from 'mongodb' import process from "next/dist/build/webpack/loaders/resolve-url-loader/lib/postcss"; const url = '내꺼....' const options = { useNewUrlParser: true } let connectDB
if (process.env.NODE_ENV === 'development') { if (!global._mongo) { global._mongo = new MongoClient(url, options).connect() } connectDB = global._mongo } else { connectDB = new MongoClient(url, options).connect() } export { connectDB }
왜그러는 걸까요 ㅠㅠ
혹시 사용하신 버전 정보를 정확히 알려주실 수 있을까요?
2023년 12월 29일 10:10 #108189
최예준참가자14 버전입니다!!
현재는
api 폴더에 post.js 추가하여 해결했습니다 ㅠㅠ
// pages/api/posts.js import { MongoClient } from 'mongodb';
export default async function handler(req, res) { const url = 'mongodb+srv://dpwns108:choiheon97@nextjs.z75zqce.mongodb.net/?retryWrites=true&w=majority'; const client = new MongoClient(url);
try { await client.connect(); const db = client.db('forum'); const posts = await db.collection('post').find().toArray(); res.status(200).json(posts); } catch (error) { res.status(500).json({ error: 'Unable to fetch data' }); } finally { await client.close(); } }
import {useEffect, useState} from "react";
export default function Page() {
const [posts, setPosts] = useState([]);
useEffect(() => { async function fetchData() { const response = await fetch('/api/posts'); const data = await response.json(); setPosts(data); }
fetchData(); }, []);
return ( <div> <h1>Posts</h1> {posts.map((post, index) => ( <div key={index}> <h2>{post.title}</h2> <p>{post.content}</p> </div> ))} </div> );
}
-
글쓴이글
6 글 보임 - 1 에서 6 까지 (총 6 중에서)
- 답변은 로그인 후 가능합니다.