查询完成后,我不确定如何关闭连接
mongoose.connect("mongodb://localhost:27017/Settings", {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true
});
SettingsModel.create({ guildID: guild.id } as Settings, (err: string, res: any) => {
if (err) return console.error(err);
console.log(res);
});
答案 0 :(得分:1)
除非有特定的原因要关闭连接,否则不应该-关闭和打开连接非常耗时且昂贵。您只需打开一次连接,即可在应用程序处于活动状态时重新使用它。
建议您在完全关闭应用程序时关闭所有连接-可以使用
process.on('SIGINT', function() {
mongoose.connection.close(function () {
console.log('Disconnected db on app termination');
process.exit(0);
});
});