Netlify 函数未连接到我的 MongoDB atlas 数据库

问题描述 投票:0回答:1

其实我对netlify并没有真正的经验,我在一个函数中遇到了一个问题,这真的很令人困惑,

我制作了一个网站,只是为了让它表现得像一个API,它现在有一个功能,它就像一个正在尝试制作的某些网站的网络服务,这是功能链接: https://omar-api.netlify.app/.netlify/functions/helpPP-webservice/add

因此,尝试连接到 mongoDB atlas 的代码,当我运行 netlify dev (测试环境)时的函数,它确实连接到 mongoDB 到我的数据库并成功返回数据,我已经做了一些基本响应来测试数据库,并且它在 netlify dev 测试环境上工作,但是当我在 netlify 上发布它时它不再工作,因此它在测试环境上工作,但是当我发布它时它不起作用。

这是代码:

‘use strict’;

const { eventNames } = require(‘process’);
const { MongoClient, ServerApiVersion } = require(‘mongodb’);

// Functions
const processForm = async function (type, body) {};

//Global vars
const uri = ‘mongodb+srv://:@omar-db.pbwn3ml.mongodb.net/?retryWrites=true&w=majority’; //for mongodb
const mainFnPath = ‘/.netlify/functions/helpPP-webservice/’;
const dbName = ‘omar-db’;
const colName = ‘helpPP-DB’;

// MongoDB setup
// Create a MongoClient with a MongoClientOptions object to set the Stable API version
const client = new MongoClient(uri, {
    serverApi: {
        version: ServerApiVersion.v1,
        strict: true,
        deprecationErrors: true,
    },
});

//

//

exports.handler = async function (event, context) {
    const path = event.path; //the called path

    //Setting up mongoDB
    try {
        // Connect the client to the server (optional starting in v4.7)
        await client.connect();
        // Send a ping to confirm a successful connection
        await client.db(dbName).command({ ping: 1 });
        console.log(‘Pinged your deployment. You successfully connected to MongoDB!’);

        //Reading the returned html
        const db = client.db(dbName);
        const col = db.collection(colName);

        //Form handle:
        if (path == mainFnPath + 'add') {
              //Checking the method
              // if (event.httpMethod == 'GET') {
              //   return {
              //     statusCode: 500,
              //     body: JSON.stringify({
              //       error: 'Form need POST request',
              //     }),
              //   };
              // }

              //Processing the form

              processForm('add', event.body);

              const query = {};
              const projection = { returnhtml: 1 };

              const data = await col.findOne(query, projection);

              return {
                  statusCode: 200,
                  body: data.returnhtml,
                  headers: {
                      'Content-type': 'text/html',
                  },
              };
        }

    } finally {
        // Ensures that the client will close when you finish/error
        console.log(‘You are no longer connected to MongoDB’);
        await client.close();
    }

    return {
        statusCode: 500,
        body: ‘ERROR! Cant show this page, please click the back button’,
        headers: {
            ‘Content-type’: ‘text/html’,
        },
    };
};

函数错误是超时,因为没有来自 MongoDB 的连接。

我已经询问了一些开发人员,但仍然不起作用。

在我的 netlify 开发测试中,响应花了 2.5 秒,但我认为在 netlify 上部署时,响应不会超过 9 秒,并且函数超时为 10 秒。

我可以只编辑包吗?或者什么?

node.js mongodb netlify netlify-function
1个回答
0
投票

我遇到了同样的问题,我通过添加新IP修复了它

您可以添加新的IP,然后单击mongoDB Atlas“网络地址”部分中的“允许从任何地方访问”按钮。

© www.soinside.com 2019 - 2024. All rights reserved.