如何通过承诺关闭 mongo 中的连接?
How to close connect in mongo by promises?
我想在 NodeJS 中为 mongodb 使用 promises。所以,我有一些代码:
const mongo = require('mongodb').MongoClient;
const config = require('./config.json');
mongo.connect(config.URI, function (err, client) {
const db = client.db("INDFLORIST");
const collection = db.collection('API');
collection.insertOne({name: 'Roger'}, function (err, res) {
if (err) throw err;
console.log("Document inserted");
client.close();
});
});
然后我将 callback
转换为 promise
:
const mongo = require('mongodb').MongoClient;
const config = require('./config.json');
mongo.connect(config.URI).then(client => {
const db = client.db("INDFLORIST");
const collection = db.collection('API');
return collection.insertOne({name: 'Roger'});
})
.then(function(result) {
console.log("Document inserted");
}).then(client => {
client.close();
})
.catch(err => {
console.error(err);
});
但是此脚本调用错误:类型错误:无法读取未定义的属性 'close'。
你能帮帮我吗?这个问题怎么解决?
您可以创建一些外部变量 _client_
并在成功连接后将 client
分配给它,然后您可以使用 _client_
在最后一个 then
回调中关闭连接
const mongo = require("mongodb").MongoClient;
const config = require("./config.json");
let _client_; // <-- external variable
mongo.connect(config.URI).then(client => {
_client_ = client; // <-- assing real client to it
const db = client.db("INDFLORIST");
const collection = db.collection("API");
return collection.insertOne({name: "Roger"});
}).then(function (result) {
console.log("Document inserted");
}).then(() => {
_client_.close(); // <-- close connection
}).catch(err => {
console.error(err);
});
您还可以将其传递给所有 then
回调
const mongo = require("mongodb").MongoClient;
const config = require("./config.json");
mongo.connect(config.URI).then(client => {
const db = client.db("INDFLORIST");
const collection = db.collection("API");
return collection.insertOne({name: "Roger"}).then(() => client);
}).then(function (client) {
console.log("Document inserted");
return client;
}).then(client => {
client.close();
}).catch(err => {
console.error(err);
});
我想在 NodeJS 中为 mongodb 使用 promises。所以,我有一些代码:
const mongo = require('mongodb').MongoClient;
const config = require('./config.json');
mongo.connect(config.URI, function (err, client) {
const db = client.db("INDFLORIST");
const collection = db.collection('API');
collection.insertOne({name: 'Roger'}, function (err, res) {
if (err) throw err;
console.log("Document inserted");
client.close();
});
});
然后我将 callback
转换为 promise
:
const mongo = require('mongodb').MongoClient;
const config = require('./config.json');
mongo.connect(config.URI).then(client => {
const db = client.db("INDFLORIST");
const collection = db.collection('API');
return collection.insertOne({name: 'Roger'});
})
.then(function(result) {
console.log("Document inserted");
}).then(client => {
client.close();
})
.catch(err => {
console.error(err);
});
但是此脚本调用错误:类型错误:无法读取未定义的属性 'close'。
你能帮帮我吗?这个问题怎么解决?
您可以创建一些外部变量 _client_
并在成功连接后将 client
分配给它,然后您可以使用 _client_
在最后一个 then
回调中关闭连接
const mongo = require("mongodb").MongoClient;
const config = require("./config.json");
let _client_; // <-- external variable
mongo.connect(config.URI).then(client => {
_client_ = client; // <-- assing real client to it
const db = client.db("INDFLORIST");
const collection = db.collection("API");
return collection.insertOne({name: "Roger"});
}).then(function (result) {
console.log("Document inserted");
}).then(() => {
_client_.close(); // <-- close connection
}).catch(err => {
console.error(err);
});
您还可以将其传递给所有 then
回调
const mongo = require("mongodb").MongoClient;
const config = require("./config.json");
mongo.connect(config.URI).then(client => {
const db = client.db("INDFLORIST");
const collection = db.collection("API");
return collection.insertOne({name: "Roger"}).then(() => client);
}).then(function (client) {
console.log("Document inserted");
return client;
}).then(client => {
client.close();
}).catch(err => {
console.error(err);
});