如何调用NextJs api从MongoDB获取数据?

发布于 2025-01-15 13:08:32 字数 1942 浏览 0 评论 0原文

我创建了一个从 MongoDb 获取数据的 products api。

import dbConnect from "../../../lib/mongodb";
import Products from "../../../models/Products";

export default async function handler(req, res) {
  const { method } = req;
  dbConnect();
  if (method === "GET") {
    try {
      const products = await Products.find();
      res.status(200).json(products);
    } catch (err) {
      res.status(500).json(err);
    }
  }

  if (method === "POST") {
    try {
      const product = await Products.create(req.body);
      res.status(201).json(product);
    } catch (err) {
      res.status(500).json(err);
    }
  }
}

我成功连接到数据库并在 localhost 中获取数据。但是当我将其部署到 vercel 上时,出现500 内部错误。我在react-admindataProvider.js中使用这个api,就像这样

import { fetchUtils } from "react-admin";

import { stringify } from "query-string";
const httpClient = fetchUtils.fetchJson;

const dataProvider = {
  getList: (resource, params) => {
    const { page, perPage } = params.pagination;
    const query = {
      range: JSON.stringify([(page - 1) * perPage, page * perPage - 1]),
    };
    const url = `/api/${resource}?${stringify(
      query
    )}`;  //here the API that I call

    return httpClient(url).then(({ headers, json }) => ({
      data: json.map((resource) => ({ ...resource, id: resource._id })),
      total: parseInt(headers.get("content-range").split("/").pop(), 10),
    }));
  },
};

export default dataProvider;

开发者工具中network的屏幕截图

这里是本地 输入图片此处描述生产中 输入图片此处描述

我想分享我的域名链接

I have created a products api that fetch data from MongoDb.

import dbConnect from "../../../lib/mongodb";
import Products from "../../../models/Products";

export default async function handler(req, res) {
  const { method } = req;
  dbConnect();
  if (method === "GET") {
    try {
      const products = await Products.find();
      res.status(200).json(products);
    } catch (err) {
      res.status(500).json(err);
    }
  }

  if (method === "POST") {
    try {
      const product = await Products.create(req.body);
      res.status(201).json(product);
    } catch (err) {
      res.status(500).json(err);
    }
  }
}

I am successfully connect to database and fetch the data in localhost.But when I deploying it on vercel, I take 500 Internal Error. I am using this api in react-admin dataProvider.js just like that

import { fetchUtils } from "react-admin";

import { stringify } from "query-string";
const httpClient = fetchUtils.fetchJson;

const dataProvider = {
  getList: (resource, params) => {
    const { page, perPage } = params.pagination;
    const query = {
      range: JSON.stringify([(page - 1) * perPage, page * perPage - 1]),
    };
    const url = `/api/${resource}?${stringify(
      query
    )}`;  //here the API that I call

    return httpClient(url).then(({ headers, json }) => ({
      data: json.map((resource) => ({ ...resource, id: resource._id })),
      total: parseInt(headers.get("content-range").split("/").pop(), 10),
    }));
  },
};

export default dataProvider;

Here the screenshots of network in developer tools

In Local
enter image description here
In Production
enter image description here

I want to share my domain link
https://electronic-products47.vercel.app/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

待天淡蓝洁白时 2025-01-22 13:08:32

我认为,在Monge DB网络访问中您需要将0.0.0.0/0 IP添加到“白名单”中,以便您的Vercel服务器不被Mongo DB数据库阻止。

输入图片此处描述

I think, in Monge DB network access You need to add 0.0.0.0/0 IP to the "whitelist" so that Your Vercel server is not being blocked by Mongo DB Database.

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文