我正从本地主机迁移到使用mongodb-atlas托管数据库。我在nodejs应用程序中更改了连接字符串,但是每次该应用程序在Atlas服务器上创建一个新文档时,仍然在localhost上创建它。
我确保新的连接字符串无处不在。我也尝试创建一个新用户。
功能代码
const mongoose = require("mongoose");
const Report = require("../models/report.js")
const { mongoURL } = require('../config.json');
mongoose.connect(mongoURL, {useNewUrlParser: true});
module.exports = {
name: 'testreport',
description: 'test report',
args: true,
usage: '<user> <reason>',
async execute(message, args) {
let rUser = message.mentions.members.first();
let rreason = args;
const report = new Report({
_id: mongoose.Types.ObjectId(),
username: rUser.user.username,
userID: rUser.id,
reason: rreason,
rUsername: message.author.username,
rID: message.author.id,
time: message.createdAt
})
report.save()
.then(result => console.log(result))
.catch(err => console.log(err));
message.reply("that report has been saved!");
},
};
架构代码
const mongoose = require("mongoose");
const { mongoURL } = require('../config.json');
mongoose.connect(mongoURL, {useNewUrlParser: true});
const reportSchema = mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
username: String,
userID: String,
reason: String,
rUsername: String,
rID: String,
time: String
}, { collection: 'Reports' })
module.exports = mongoose.model("Report", reportSchema);
连接字符串:
“ mongodb + srv:// user:pass@discordbot-rpuzu.mongodb.net/test?retryWrites = true”
我希望我的地图集数据库会收到一个新的报告文档,但是该文档正在我的本地主机数据库上创建。
编辑: 我已经开始工作了,但是每次我启动应用程序时,我都会得到一个
UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [localhost:27017] on first connect
我不明白为什么强制退出本地主机可以解决此问题,为什么我仍然收到本地主机未启动并正在运行的警告。