problem
The following code does work.
const MONGODB_URL = `mongodb://${process.env.DB_USERNAME}:${process.env.DB_PASSWORD}@localhost:27017`;
mongoose.connect(MONGODB_URL);
async () => {
console.log("Cron Job Working");
try {
const result = await dummyData.save();
console.log("create succeeded");
} catch (e) {
console.log(e);
}
But the following code doesn't work.
const MONGODB_URL = `mongodb://${process.env.DB_USERNAME}:${process.env.DB_PASSWORD}@localhost:27017/db`;
mongoose.connect(MONGODB_URL);
async () => {
console.log("Cron Job Working");
try {
const result = await dummyData.save();
console.log("create succeeded");
} catch (e) {
console.log(e);
}
cause of problem
URL is needed additional parameter of authSource
when you point out db name explicitly.
Correct URL
const MONGODB_URL = `mongodb://${process.env.DB_USERNAME}:${process.env.DB_PASSWORD}@localhost:27017/world_info?authSource=admin`;
Happy coding!
コメント