This is the code which I've written:
const mongoose = require('mongoose')
const userSchema = require('./userSchema.js')
const User = mongoose.model('user', userSchema, 'user')
async function createUser(username) {
return new User({
username,
created: Date.now()
}).save()
}
async function findUser(username) {
return await User.findOne({
username
})
}
;
(async() => {
const connector = mongoose.connect(connectionString)
const username = process.argv[2].split('=')[1]
let user = await connector.then(async() => {
return findUser(username)
}).catch(console.log("Error found"))
if (!user) {
user = await createUser(username)
}
console.log(user)
process.exit(0)
})()
But the following error is coming. i couldn't figure out what is going wrong. Any idea?
Try this command
sudo service mongod start
Then nodemon
or node app.js
to start server.
Please check your connection string. You can obtain the connection string from Mongo atlas.
const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const app = express();
// Connecting to the database
mongoose.connect('mongodb:uri',{ useNewUrlParser: true } ).then(() => {
console.log("Successfully connected to the database");
}).catch(err => {
console.log('Could not connect to the database. Exiting now...');
process.exit();
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.