Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You have two problems:</p> <ol> <li><code>findBooks</code> tries to access <code>response</code>, but <code>response</code> is not in scope at that point.</li> <li>You're calling <code>findBooks</code> with <code>err</code> and <code>books</code> and passing the return value to <code>BookModel.find</code>, when you probably want to pass the function for <code>find</code> to call.</li> </ol> <p>Let's first solve the second problem. Rather than calling the function, just pass it:</p> <pre><code>return BookModel.find(findBooks); </code></pre> <p>That was easy. Now, to solve the first problem, we'll need to add an extra argument to <code>findBooks</code>. I'll assume you put it at the start for reasons I'll detail later. Then we can change the usage to look like this:</p> <pre><code>return BookModel.find(function (err, books) { return findBooks(response, err, books); }); </code></pre> <p>It turns out, though, that there's a more concise way to do that, using <code>bind</code>. <code>bind</code> is best known for its use in binding the value of <code>this</code>, but it can also be used to bind arguments:</p> <pre><code>return BookModel.find(findBooks.bind(null, response)); </code></pre> <p>All together, we get:</p> <pre><code>function findBooks (response, err, books) { if (!err) { return response.send(books); } else { console.log(err); } } // Get all books app.get('/api/books', function (request, response) { return BookModel.find( findBooks.bind(null, response) ); }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload