Chapter 7: prettier

This commit is contained in:
Beth Griggs
2020-05-20 12:05:00 +01:00
parent a0cf6a254c
commit 85007a6879
12 changed files with 269 additions and 262 deletions
+36 -31
View File
@@ -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);
});
}