0

In my app.js file, I have and app.post and it is firing all of the functions I am calling every time a post request goes through. How do I set this up to not fire all of them every time?

app.post('/ownerPanel', (req, res) => {
 if(books.books.addBookOwner(req.body.book_title,req.body.author, 
    req.body.price,req.body.subject,req.body.isbn, req.body.Vendor, 
    req.body.Owner, connection, res)){
    console.log("in the if in app vendor");
    books.books.renderOwner(connection, res);
   }
else if(books.books.deleteBook(req.body.isbn,connection, res)){
  console.log("in the if in app post ");
  books.books.renderOwner(connection, res);
    }
    books.books.renderOwner(connection, res);
})

It isn't going into any of my if statements either because the console log never shows.

 renderOwner : function(connection, res){
   var out = []
  var query = connection.query('SELECT * FROM book_table WHERE Owner = "test"',function(err,rows){
   if(err)
    console.log("Error Selecting : %s ",err );

   for(var i = 0; i< rows.length; i++){
      var row = rows[i];
      out.push(row[i]);
      }
    res.render('../views/ownerPannel', {book_data : rows}) 
    });
  },

addBookOwner : function(book_title, author, price, subject, isbn, Vendor, Owner, connection, res) {
         sql = "INSERT INTO book_table (book_title,author,price,subject,isbn,Vendor,Owner) VALUES ('" + book_title+ "','" + author + "','" + price + "','" + subject + "','" + isbn +"','" + Vendor +"','" + Owner +"')";
  account = connection.query(sql, function (err, result) {
  if (err){ console.log(err);
     throw err;}
    return result
   });
  },

 deleteBook : function(isbn, connection, res){
  connection.query('DELETE FROM book_table WHERE isbn = ?', isbn , (err, result) => {
  if (err) {
    console.log(err);
   } else {
  console.log("deleted");
      }
    });
 }
};
Mitch Mo
  • 1
  • 1

1 Answers1

0

If neither of the console.log() statements in your if/else if statement are logging, then one of these things must be true:

  1. Neither of the conditions for your if or else if must be returning a truthy value.
  2. Somewhere an exception is being thrown so the if/else loop gets bypassed.

Also, your logic looks like it will call books.books.renderOwner(connection, res); twice if it does execute either the if or else if block so that is likely not what you intend either.

You can further debug exactly what's happening with this type of code:

app.post('/ownerPanel', (req, res) => {
    try {
        let addBook = books.books.addBookOwner(req.body.book_title, req.body.author,
            req.body.price, req.body.subject, req.body.isbn, req.body.Vendor,
            req.body.Owner, connection, res);
        console.log("addBook", addBook);
        if (addBook) {
            console.log("in the if in app vendor");
            books.books.renderOwner(connection, res);
        } else {
            let deleteBook = books.books.deleteBook(req.body.isbn, connection, res);
            console.log("deleteBook", deleteBook);
            if (deleteBook) {
                console.log("in the if in app post ");
                books.books.renderOwner(connection, res);
            } else {
                console.log("didn't match either if or else if conditions");
            }
        }
        books.books.renderOwner(connection, res);
    } catch(e) {
        console.log("ownerPanel exception", e);
        res.sendStatus(500);
    }
})

To help you further, we need more debug info and probably need to know more about what you're actually trying to accomplish with this request handler and with these book APIs.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • When I ran it with the addBook post request it read back to me addBook undefined deleteBook undefined didn't match either if or else if conditions deleted "Deleted" is inside my console log for inside if it works successfuly books.books.deleteBook(req.body.isbn, connection, res);deleteBook : function(isbn, connection, res){ connection.query('DELETE FROM book_table WHERE isbn = ?', isbn , (err, result) => { if (err) { console.log(err); } else { console.log("deleted"); } }); } }; So it is running deleteBook anyway? – Mitch Mo Nov 26 '18 at 01:29
  • BUT just doing the post request form for deleteBook works perfectly – Mitch Mo Nov 26 '18 at 01:32
  • @MitchMo - Then it sounds like your methods `.addBookOwner()` and `.deleteBook()` must not be returning anything so using them in a `if` statement like you are is the wrong way to program with them. I would guess that they are actually asynchronous and you would need to use a callback with them to know when they are actually done. Please share a link to the documentation for those. – jfriend00 Nov 26 '18 at 01:33
  • What is the best way to share a link to those methods? is there a website I could use to post them up? – Mitch Mo Nov 26 '18 at 02:17
  • @MitchMo - Are they your methods? If so, just add the code to your question - use the 'Edit" link to modify your question. – jfriend00 Nov 26 '18 at 03:06
  • @MitchMo - You need to read [How do I return the response from an asynchronous call](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call/14220323#14220323). Your methods are doing a return to the async callback, not to the function itself. Your methods themselves have no return value and you can't return the async value from those methods. Your methods will need to return a promise (that is resolved when the async operations are done) or will need to use a callback themselves. – jfriend00 Nov 26 '18 at 03:44