#node.js #mongodb #mongoose
Вопрос:
Я использую MongoDB в своем приложении Nodejs с mongoose
as ORM. Однако я получаю эту ошибку каждый раз, когда запускаю свой код.
(node:28316) UnhandledPromiseRejectionWarning: MongooseError: Operation `auths.findOne()` buffering timed out after 10000ms
at Timeout.<anonymous> (/home/mridu/Projects/unoletter/node_modules/mongoose/lib/drivers/node-mongodb-native/collection.js:149:23)
at listOnTimeout (internal/timers.js:557:17)
at processTimers (internal/timers.js:500:7)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:28316) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
(node:28316) [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.
Вот мой код для подключения к MongoDB atlast
try {
mongoose.connect(
process.env.DB_URI,
{
keepAlive: true,
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
},
() => {
console.log(`connected to MongoDB`);
}
);
} catch (error) {
console.log(error);
}
Моя модель
//Dependencies
const mongoose = require("mongoose");
//Auth Model
const auth = {
id: {
type: String,
required: true,
},
email: {
type: String,
required: true,
},
secret: {
type: String,
required: true,
},
isVerified: Boolean,
createdAt: {
time: String,
date: String,
},
};
const Auth = mongoose.model("Auth", auth);
//Export
module.exports = { Auth };
И вот как я сохраняю свои данные
const auth = new Auth({
id: generateId(),
email: body.email,
secret: generateSecret(),
isVerified: false,
createdAt: {
time: dayjs().format("HH:mm:ss"),
date: dayjs().format("YYYY/MM/DD"),
},
});
try {
await auth.save();
console.log(auth);
} catch (error) {
console.log(error);
}
Я попробовал некоторые ответы с Stackoverflow и других сайтов, таких как Dev.to. Я пробовал использовать async/await
, .then amp; .catch
. И все же я сталкиваюсь с той же ошибкой.
Другой проект, в котором я использую 5.xx
версию Мангуста, работает нормально. Я столкнулся с этой проблемой в новой 6.0.3
версии мангуста.
Комментарии:
1. там было сказано
Operation auths.findOne() buffering timed out after 10000ms
, но вы определяетеauth
2. другое дело, может быть, отправить запрос монго перед подключением