Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
TEACHING-MATERIALS/exercises/07_persistence/document/mongodb.js
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
55 lines (47 sloc)
1.32 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
const request = require('request') | |
const Book = require('./bookSchema') | |
exports.addBook = function(isbn, callback) { | |
const url = `https://www.googleapis.com/books/v1/volumes?q=isbn:${isbn}` | |
request.get( url, (err, res, body) => { | |
if (err) return callback(Error('could not complete request')) | |
const json = JSON.parse(body) | |
if (json.totalItems === 0) { | |
console.log('no results') | |
return callback(Error('book not found')) | |
} | |
const data = { | |
title: `${json.items[0].volumeInfo.title}: ${json.items[0].volumeInfo.subtitle}`, | |
authors: json.items[0].volumeInfo.authors[0], | |
description: json.items[0].volumeInfo.description | |
} | |
console.log('define the book to save') | |
const book = new Book({ | |
title: data.title, | |
authors: data.authors, | |
description: data.description | |
}) | |
console.log('saving the book') | |
book.save( function(err, book) { | |
console.log('attempt made') | |
if (err) { | |
console.log('an error saving book') | |
callback( Error(`database error: ${err}`) ) | |
} | |
console.log('book saved') | |
console.log(book) | |
return callback(null, book) | |
}) | |
}) | |
} | |
exports.getAll = function() { | |
console.log('retrieving all the books') | |
Book.find({}, function(err, books) { | |
if (err) { | |
console.log('error getting books') | |
throw err | |
} | |
// object of all the users | |
console.log(books) | |
}) | |
} |