gpt4 book ai didi

node.js - 等待 promise

转载 作者:行者123 更新时间:2023-12-03 01:18:22 24 4
gpt4 key购买 nike

我正在尝试使用 this 中的代码答案是扩展一个OSS应用程序。

但是 app.js 是同步的,无论我做什么,我都不能强制它等待 promise 解决。

app.js

var cosmos = require('./cosmos.js');
const key = cosmos.key(var1, var2, var3);
console.log(key); // << shows Promise { <pending> }

mongoose.connect(`redacted`, {
auth: {
username: config.database.name,
password: key
}
});

cosmos.js

async function retriveKey(subId, resGrp, server) {
const { EnvironmentCredential } = require("@azure/identity");
const { CosmosDBManagementClient } = require("@azure/arm-cosmosdb");
const armClient = new CosmosDBManagementClient(
new EnvironmentCredential(), subId
);

const { primaryMasterKey } = await armClient.databaseAccounts.listKeys(
resGrp, server
);
return new Promise(resolve => {
setTimeout(() => resolve(primaryMasterKey), 1000);
});
}

exports.key = retriveKey

如果我在异步函数中使用console.log(),它实际上会显示 key ,但是 Mongoose 数据库连接不会等待 promise 得到解决,它会立即开始连接并失败,并显示以下内容:password必须是字符串

如果我硬编码实际 key 而不是这个 promise - 一切正常。

编辑:

一半:

// pull cosmos keys
async function retriveKey(subId, resGrp, server) {
const { EnvironmentCredential } = require("@azure/identity");
const { CosmosDBManagementClient } = require("@azure/arm-cosmosdb");
const armClient = new CosmosDBManagementClient(
new EnvironmentCredential(), subId
);
const { primaryMasterKey } = await armClient.databaseAccounts.listKeys(
resGrp, server
);
return primaryMasterKey // don't even need a promise here
}

exports.key = retriveKey
var mongooseConnected; // global variable
app.use(function (req, res, next) {
if (!moongooseConnected) {
moongooseConnected = cosmos.key(var1, var2, var3).then(function (key) {
mongoose.connect(`xxx`,
{
auth: {
username: config.database.name,
password: key
}
}
);
console.log(key); // works as expected

require('./models/user');
require('./models/audit');

require('./routes/user')(app);
require('./routes/audit')(app, io);
});
}
moongooseConnected.then(function () {
next();
});
});

数据库连接已建立,console.log(key) 在日志中显示正确的 key ,但应用程序中不存在任何路由。

如果我将路线或模型移到此 app.use(xyz) 之外 - 我开始看到由于以下原因而导致的失败:

Connection 0 was disconnected when calling createCollection

MongooseError [MissingSchemaError]: Schema hasn't been registered for model "User".

这(我认为)意味着它们需要实例化 Mongoose ,但它们不会等待。

最佳答案

如果您从 CommonJS 模块切换到 ES modules ,您可以使用 await 等待 Promise 解析:

import cosmos from './cosmos.js';
const key = await cosmos.key(var1, var2, var3);
console.log(key);
await mongoose.connect(`redacted`, {
auth: {
username: config.database.name,
password: key
}
});

或者,您可以等待 mongoose 的初始化,直到第一个请求到来,因为 Express 中间件是异步的:

var mongooseConnected; // global variable
function connectMongoose() {
if (!mongooseConnected)
mongooseConnected = cosmos.key(var1, var2, var3)
.then(key => mongoose.connect(`redacted`, {
auth: {
username: config.database.name,
password: key
}
}));
return mongooseConnected;
}
module.exports = connectMongoose;

如果其他地方需要上面的代码,可以将其放在单独的模块中并在需要的地方导入:

const connectMongoose = require("./connectMongoose");
app.use(function(req, res, next) {
connectMongoose().then(function() {
next();
});
});
require('./routes/user')(app);
require('./routes/audit')(app, io);

请注意,如果有多个并行请求进来,则只有第一个请求会让全局变量 mongooseConnected 等于一个 Promise,所有这些请求将在调用 next 之前等待它解析().

另请注意,app 的其他路由必须在此 app.use 命令之后注册,而不是在其中注册。

关于node.js - 等待 promise ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73048205/

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