gpt4 book ai didi

node.js - 错误 - 使用 Mongoose 连接到 Azure CosmosDB

转载 作者:太空宇宙 更新时间:2023-11-04 01:50:54 25 4
gpt4 key购买 nike

我正在尝试使用 mongoose 连接到 Node.js 中的 Microsoft Azure CosmosDB 模拟器。这是我正在使用的代码:

var mongoose = require('mongoose');

mongoose.Promise = global.Promise;

mongoose.connect("mongodb://localhost:C2y6yDjf5%2FR%2Bob0N8A7Cgv30VRDJIWEHLM%2B4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw%2FJw%3D%3D@localhost:8081/cryptam?ssl=true");

这是我收到的错误:

(node:6572) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): #<Object>
(node:6572) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

如果我在没有 url 编码的情况下使用它,这就是我得到的错误:

(node:8088) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): Error: Password contains an illegal unescaped character
(node:8088) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

那么,如何修复此错误以使用 Mongoose 连接到本地 Azure Cosmos DB 模拟器?

最佳答案

尝试以下代码,

const mongoose = require('mongoose');
mongoose.Promise = global.Promise;

const mongoUri = `mongodb://${process.env.COSMOSDB_ACCOUNT}:${process.env.COSMOSDB_KEY}@${
process.env.COSMOSDB_ACCOUNT
}.documents.azure.com:${process.env.COSMOSDB_PORT}/${process.env.COSMOSDB_DB}?ssl=true`;


// Local MongoDB Connection String
// const mongoUri = `mongodb://localhost:27017/connect-todoes`;

function connect() {
mongoose.set('debug', true);
return mongoose.connect(mongoUri, { useMongoClient: true });
}

module.exports = {
connect,
mongoose
};

关于node.js - 错误 - 使用 Mongoose 连接到 Azure CosmosDB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49721425/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com