我正在尝试通过服务器连接到猫鼬,但没有成功。
我已经尝试将其放入try catch块中,但是它说db.close或client.close不是函数。尝试连接时,出现错误“未处理的承诺拒绝警告”。
如果有人可以帮助我,我将很高兴
const mongoose = require('mongoose');
mongoose.Promise = global.Promise;
const client = mongoose.connect('mongodb://localhost:27017/customercli', {
useNewUrlParser: true });
// import the model
const Customer = require('./models/customer');
// Add customer
const addCustomer = (customer) => {
Customer.create(customer).then(customer => {
console.info("New Customer Added");
client.close();
});
}
// Find customer
const findCustomer = (name) => {
//Make this case insensitive
const search = new RegExp(name, 'i') // here lowercase i made it
insensitive
Customer.find({$or: [{firstname: search}, {lastname: search}]})
.then(customer => {
console.log(customer);
console.log(`${customer.length} matches`);
client.close();
});
}
//Export all methods
module.exports = {
addCustomer,
findCustomer
}
答案 0 :(得分:0)
我自己找到了解决方法。通过我的服务器与猫鼬的连接未关闭。所以我用db.close()
取代了mongoose.connection.close()
,效果很好。