-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauthorsAndBooks.js
52 lines (44 loc) · 1.02 KB
/
authorsAndBooks.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
const Book = require("./models/book");
const Author = require("./models/author");
const author = new Author({
name: "Kyle Simpson",
age: 44
});
const book = new Book({
name: "You Dont know JS",
genre: "Programming",
isbn: "12343234354566332",
year: 2015,
authorId: "5db4d553198df2526728597a"
});
async function addAuthor() {
return author
.save()
.then(authorData => authorData)
.catch(err => console.log("An error occured", err));
}
async function addBook() {
return book
.save()
.then(bookData => bookData)
.catch(err => console.log("An error occured", err));
}
function listAllAuthors() {
Author.find()
.exec()
.then(authors => console.log("Authors", authors))
.catch(err => console.log(err));
}
function listAllBooks() {
Book.find()
.exec()
.then(books => console.log("Books", books))
.catch(err => console.log(err));
}
async function addDataToDB() {
await addAuthor();
listAllAuthors();
await addBook();
listAllBooks();
}
module.exports = addDataToDB;