0

I am not sure what exactly oop style of nodejs is called, so please pardon for me that and let me know if you know exact word for this.

So comming to my question, I am proficient with nodejs and have used async await a lot in my previous codes. But now I need to migrate my code to oop style of coding (may be typescript style? ). So I am really not sure how I can use async await with this. Here is what I have tried till now:

==server.js

import bookController from './routes/books/bookController';
import bookService from './routes/books/bookService';
import bookQueries from './routes/books/bookQueries';

let bookCtrl = new bookController(bookService, bookQueries);

app.get('/books/allbooks', (request, response) => {
  bookCtrl.getAllBooks(request, response);
});

== bookController.js

export default class bookController extends baseController {
  constructor(bookService, bookQueries){
    super();
    this.bookService = new bookService();
    this.bookQueries = new bookQueries();
  }

  async getAllBooks(request, response){ // Using async here throws error
     var conn = await this.getBookConn();
     var result = await conn.query("select * from books");
     response.status(200).json(result);
  }
}

== Base Controller **

import mysql from 'promise-mysql';
export default class baseController {
    constructor(){
    }
    getBookConn(){
        let bzConn = mysql.createConnection({
            host: 'abc.com',
            database: 'test',
            user: 'test',
            password: 'test',
            insecureAuth: true
        })
        return bzConn;
    }
}

**

undefined
  • 3,464
  • 11
  • 48
  • 90
  • What specific error are you getting ? – Titian Cernicova-Dragomir Sep 14 '17 at 11:34
  • I have no error using async as a method modifier in nodejs 7.10. What version of node are you using and what the error do you have ? Could be this related with `baseController` ? – Titouan56 Sep 14 '17 at 11:34
  • I am using node v8. and this is the error I am getting: Debugger listening on ws://127.0.0.1:9229/093d62f7-0570-4434-860f-5df650e4c9a6 For help see https://nodejs.org/en/docs/inspector ReferenceError: regeneratorRuntime is not defined – undefined Sep 14 '17 at 11:36
  • 1
    "I need to migrate my code to oop style of coding" why? What possible benefit is that going to give you? – Jared Smith Sep 14 '17 at 11:37
  • @JaredSmith I really do not know and do not find any benefit.. Honestly speaking just because my leader want us to do that. His reasoning is code will be more cleaner this way. – undefined Sep 14 '17 at 11:39
  • This issue looks related to "babel" are you transpiling your code using babel ? – Titouan56 Sep 14 '17 at 11:39
  • yes, and I have already looked into this issue but I have already taken care of this https://stackoverflow.com/questions/33527653/babel-6-regeneratorruntime-is-not-defined – undefined Sep 14 '17 at 11:42
  • Your "leader" is possibly an idiot, but yeah them's the breaks. More to the point, quit using babel or include the regenerator plugin. If you'd already taken care of it, you wouldn't have that error. – Jared Smith Sep 14 '17 at 11:42

0 Answers0