title: KoaJS with Thunkify + Mongoose
body: <p>I'm working on my first koajs project and after understanding (or trying to) generator functions, yield, thunks etc. I'm running into a problem with mongoose (on which I heavily rely). After searching the forums, the solution was thunkify:</p>
  
  <p><strong>CoffeeScript:</strong></p>
  
  <pre><code>Controller = require './basecontroller'
  User = require '../models/user'
  
  UserController = ((c, User) -&gt;
    return (
      get: (next) -&gt;
       userQuery = c.libs.thunkify User.find
       try
         users = yield userQuery {}
         @body = users
         return
       catch e
         console.error  "Error: #{e}"
         throw e
         return
       next()
    )
  ) Controller, User
  
  # Export functionality
  module.exports = UserController
  </code></pre>
  
  <p><strong>JavaScript:</strong></p>
  
  <pre><code>// Generated by CoffeeScript 1.8.0
  var Controller, User, UserController;
  
  Controller = require('./basecontroller');
  
  User = require('../models/user');
  
  UserController = (function(c, User) {
    return {
      get: function*(next) {
        var e, userQuery, users;
        userQuery = c.libs.thunkify(User.find);
        try {
          users = (yield userQuery({}));
          this.body = users;
          return;
        } catch (_error) {
          e = _error;
          console.error("Error: " + e);
          throw e;
          return;
        }
        return next();
      }
    };
  })(Controller, User);
  
  module.exports = UserController;
  </code></pre>
  
  <p>which makes sense in my head ... but I'm receiving this error:</p>
  
  <pre><code>14:56:01 web.1  | Error: TypeError: Cannot read property 'discriminatorMapping' of undefined
  14:56:01 web.1  |   TypeError: Cannot read property 'discriminatorMapping' of undefined
  14:56:01 web.1  |       at find (/Users/mial/Projekte/MiPa/schuppen4-on-heroku/node_modules/mongoose/lib/model.js:940:18)
  14:56:01 web.1  |       at Object.&lt;anonymous&gt; (/Users/mial/Projekte/MiPa/schuppen4-on-heroku/node_modules/thunkify/index.js:43:12)
  14:56:01 web.1  |       at next (/Users/mial/Projekte/MiPa/schuppen4-on-heroku/node_modules/koa/node_modules/co/index.js:90:21)
  14:56:01 web.1  |       at Object.&lt;anonymous&gt; (/Users/mial/Projekte/MiPa/schuppen4-on-heroku/node_modules/koa/node_modules/co/index.js:45:5)
  14:56:01 web.1  |       at Server.&lt;anonymous&gt; (/Users/mial/Projekte/MiPa/schuppen4-on-heroku/node_modules/koa/lib/application.js:125:8)
  14:56:01 web.1  |       at Server.emit (events.js:110:17)
  14:56:01 web.1  |       at HTTPParser.parserOnIncoming [as onIncoming] (_http_server.js:491:12)
  14:56:01 web.1  |       at HTTPParser.parserOnHeadersComplete (_http_common.js:111:23)
  14:56:01 web.1  |       at Socket.socketOnData (_http_server.js:343:22)
  14:56:01 web.1  |       at Socket.emit (events.js:107:17)
  </code></pre>
  
  <p>... umm. Help? :)</p>