1

目前使用 MongoDB 出现以下错误:

no saslprep library specified. Passwords will not be sanitized

我们正在使用 Webpack,所以简单地安装模块是行不通的(Webpack 只是忽略它)。我发现这个线程讨论了如何将它从 Webpack 编译中排除,但是我必须手动将它加载到每个 Lambda 函数中,这导致我使用 Lambda 层。

按照关于使用 Lambda 层的无服务器指南,我可以将我的层发布到 AWS 并包含在我的所有函数中,但由于某种原因,它没有安装模块。如果我使用 AWS GUI 下载该层,我会得到一个仅包含package.jsonpackage-lock.json文件的文件夹。

我的文件结构是:

my-project
|_ layers
    |_ saslprep
       |_ package.json

serverless.yml的是:

layers:
    saslprep:
      path: layers/saslprep
      compatibleRuntimes:
        - nodejs14.x
4

1 回答 1

0

这不是我的首选解决方案,因为我想使用 256,但我解决此错误/警告的方法是将连接字符串中的 authMechanism 从 SCRAM-SHA-256 更改为 SCRAM-SHA-1。serverless-bundle 很可能需要将此依赖项添加到他们的包中以启用对 Mongo 4.0 SHA256 的支持(我最好的猜测!)。

您可以通过将 authMechanism 参数设置为连接字符串中的值 SCRAM-SHA-1 来指定此身份验证机制,如以下示例代码所示。

const { MongoClient } = require("mongodb");

// Replace the following with values for your environment.
const username = encodeURIComponent("<username>");
const password = encodeURIComponent("<password>");
const clusterUrl = "<MongoDB cluster url>";

const authMechanism = "SCRAM-SHA-1";

// Replace the following with your MongoDB deployment's connection string.
const uri =
  `mongodb+srv://${username}:${password}@${clusterUrl}/?authMechanism=${authMechanism}`;

// Create a new MongoClient
const client = new MongoClient(uri);

// Function to connect to the server
async function run() {
  try {
    // Connect the client to the server
    await client.connect();

    // Establish and verify connection
    await client.db("admin").command({ ping: 1 });
    console.log("Connected successfully to server");
  } finally {
    // Ensures that the client will close when you finish/error
    await client.close();
  }
}
run().catch(console.dir);
于 2021-06-29T19:37:30.407 回答