hi every one . i’m new member , this is my API code to connect to my Mongodb
import { MongoClient } from "mongodb";
async function haandler(req, res) {
console.log("first");
if (req.method === "POST") {
const data = req.body;
console.log(data);
const client = await MongoClient.connect(
"mongodb+srv://zahra13tu:rRjjB9dIA4kbcYio@cluster0.df9aj2h.mongodb.net/quiz?retryWrites=true&w=majority"
);
const db = client.db();
const dataCollection = db.collection("authentication");
const result = await dataCollection.insertOne( data );
console.log(result);
client.close();
res.status(201).json("data saved succesfully");
}
}
export default haandler;
**I tried to change IP to access from every where **
every time i try to send request i got this error :
settle.js:19 Uncaught (in promise)
1. AxiosError {message: 'Request failed with status code 500', name: 'AxiosError', code: 'ERR_BAD_RESPONSE', config: {…}, request: XMLHttpRequest, …}
1. code: "ERR_BAD_RESPONSE"
2. config: {transitional: {…}, adapter: Array(2), transformRequest: Array(1), transformResponse: Array(1), timeout: 0, …}
3. message: "Request failed with status code 500"
4. name: "AxiosError"
5. request: XMLHttpRequest {onreadystatechange: null, readyState: 4, timeout: 0, withCredentials: false, upload: XMLHttpRequestUpload, …}
6. response: {data: '<!DOCTYPE html><html><head><style data-next-hide-f…p":true,"scriptLoader":[]}\x3C/script></body></html>', status: 500, statusText: 'Internal Server Error', headers: AxiosHeaders, config: {…}, …}
7. stack: "AxiosError: Request failed with status code 500\n at settle (webpack-internal:///./node_modules/axios/lib/core/settle.js:24:12)\n at XMLHttpRequest.onloadend (webpack-internal:///./node_modules/axios/lib/adapters/xhr.js:125:66)"
and this is the code that is sending data to API :
import FormItem from "../../components/form";
import axios from "axios";
const Authentication = () => {
const dataGether = async (data: {
name: string;
email: string;
password: string;
score: number;
}) => {
const response = await axios.post("/api/pj-api", { data });
};
return <FormItem onDataGetter={dataGether} />;
};
export default Authentication;
I also have this error in my terminal : MongoServerSelectionError: connect ETIMEDOUT 34.231.187.91:27017
it seems i cant connect to my mongo but i dont know why