Chapter 7: prettier
This commit is contained in:
@@ -1,42 +1,47 @@
|
||||
const {
|
||||
MongoClient
|
||||
} = require('mongodb')
|
||||
const task = process.argv[2]
|
||||
const { MongoClient } = require("mongodb");
|
||||
const task = process.argv[2];
|
||||
|
||||
const URI = 'mongodb://localhost:27017/'
|
||||
const URI = "mongodb://localhost:27017/";
|
||||
|
||||
MongoClient.connect(URI, {
|
||||
useUnifiedTopology: true
|
||||
}, connected)
|
||||
MongoClient.connect(
|
||||
URI,
|
||||
{
|
||||
useUnifiedTopology: true,
|
||||
},
|
||||
connected
|
||||
);
|
||||
|
||||
function connected(err, client) {
|
||||
if (err) throw err
|
||||
const tasks = client.db('tasklist').collection('tasks')
|
||||
if (err) throw err;
|
||||
const tasks = client.db("tasklist").collection("tasks");
|
||||
|
||||
if (task) {
|
||||
addTask(client, tasks)
|
||||
} else {
|
||||
listTasks(client, tasks)
|
||||
}
|
||||
if (task) {
|
||||
addTask(client, tasks);
|
||||
} else {
|
||||
listTasks(client, tasks);
|
||||
}
|
||||
}
|
||||
|
||||
function addTask(client, tasks) {
|
||||
tasks.insertOne({
|
||||
task: task
|
||||
}, (err) => {
|
||||
if (err) throw err
|
||||
console.log("New Task: ", task);
|
||||
listTasks(client, tasks)
|
||||
})
|
||||
tasks.insertOne(
|
||||
{
|
||||
task: task,
|
||||
},
|
||||
(err) => {
|
||||
if (err) throw err;
|
||||
console.log("New Task: ", task);
|
||||
listTasks(client, tasks);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
function listTasks(client, tasks) {
|
||||
tasks.find().each((err, doc) => {
|
||||
if (err) throw err
|
||||
if (!doc) {
|
||||
client.close()
|
||||
return
|
||||
}
|
||||
console.log(doc)
|
||||
})
|
||||
}
|
||||
tasks.find().each((err, doc) => {
|
||||
if (err) throw err;
|
||||
if (!doc) {
|
||||
client.close();
|
||||
return;
|
||||
}
|
||||
console.log(doc);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user