-2

I am trying to return last inserted value using model. my function is

 async addBook(data)
    {
        await db.BOOK.create(data).then((BOOK) => {
            console.log("BOOK_KEY",BOOK.BOOK_KEY);
            return BOOK.BOOK_KEY;
        })
    }

BOOK_KEY is not printing.

let bookId = this.addBook(data)
 console.log("bookId", bookId);

and Here bookId is going state

bookId Promise { <pending> }

Kindly help me to display bookId. Thanks in advance.

sonal
  • 1
  • 1
    Does this answer your question? [How to return the response from an asynchronous call](https://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) – jabaa Nov 24 '21 at 14:02
  • `let bookId = this.addBook(data)` doesn't work. `addBook` doesn't contain a return statement and doesn't return anything explicitly (it returns `undefined` implicitly). – jabaa Nov 24 '21 at 14:03

1 Answers1

0

async function inherently returns a Promise, so you need to either use a .then or await to get the data.

async addBook(data) {
   const BOOK = await db.BOOK.create(data);
   return BOOK.BOOK_KEY;
}
    

let bookId = await this.addBook(data);
console.log("bookId", bookId);
    
Kanishk Anand
  • 1,686
  • 1
  • 7
  • 16