0

This is a Books Donation project and I am trying to write some methods in async/await way.

BooksRepo :class that has all the methods

class BooksRepo {
    constructor() {
        this.fse = require('fs-extra');
        this.catalogFilePath = '../data/catalog.books.json';
    }


    async readFileAsync(filePath) {
        let data = await this.fse.readFile(filePath);
        let parsedData = await JSON.parse(data);

        return parsedData;
    }



    async getBook(bookName) {
        let books = await this.readFileAsync(this.catalogFilePath);
        let book = books.find(b => b.title == bookName);
        return book;
    }

}

catalogFile: Example of how it structured

[
  {
    "_id": 1,
    "title": "Unlocking Android",
    "isbn": "1933988673",
    "pageCount": 416,
    "publishedDate": {
      "$date": "2009-04-01T00:00:00.000-0700"
    },
    "thumbnailUrl": "https://s3.amazonaws.com/AKIAJC5RLADLUMVRPFDQ.book-thumb-images/ableson.jpg",
    "shortDescription": "Unlocking Android: A Developer's Guide provides concise...",
    "longDescription": "Android is an open source mobile phone platform based on the Linux operating ...",
    "status": "PUBLISH",
    "authors": [
      "W. Frank Ableson",
      "Charlie Collins",
      "Robi Sen"
    ],
    "categories": [
      "Open Source",
      "Mobile"
    ]
  },

app : class this is how I am trying to call the methods

let booksRepo = new BooksRepo();
    booksRepo.readFileAsync(booksRepo.catalogFilePath).then(result=> console.log(result));

    let books = booksRepo.getBook("Android in Action, Second Edition");

    console.log(books)

I get this error BooksRepo is not defined

I don't know where is the problem

  • 1
    Please tag this question with the language you're using. – Wai Ha Lee Mar 03 '19 at 09:34
  • 1
    Seems like you are not exporting or importing (or both) the `BooksRepo` class. Thou it's hard to tell as we can't see the entire files – DSCH Mar 03 '19 at 09:44
  • @DSCH I did export it and now it gives me this error Promise { } (node:71617) UnhandledPromiseRejectionWarning: Error: ENOENT: no such file or directory, open '../data/catalog.books.json' –  Mar 03 '19 at 09:48
  • 3
    That means that `'../data/catalog.books.json'` is not the correct path to the file. – Lyle Underwood Mar 03 '19 at 09:54
  • 1
    @LyleUnderwood okay thanks I corrected the path and it worked :) –  Mar 03 '19 at 10:04
  • `JSON.parse` is not asynchronous, you should not `await` it. – Bergi Mar 03 '19 at 13:17
  • Btw, it's probably not a good idea to read the file again on every `getBook` call. Instead, make the data a property of the `BookRepo` instance and [read the file on initialisation, before constructing the object](https://stackoverflow.com/a/24686979/1048572). – Bergi Mar 03 '19 at 13:20

1 Answers1

-1

It looks you have a problem with exports/imports.

First, you have to export the class in the file it is in:

module.exports = BooksRepo;

Then to import it in the file where it's used:

var BooksRepo = require('./books-repo');

Alternatively:

export default class BooksRepo { ...

And

import BooksRepo from './books-repo.js';
Barney
  • 797
  • 1
  • 11
  • 20