2014-10-03 98 views
1

我正在研究我的第一個koajs項目,並理解(或嘗試)發電機功能,產量,thunks等。我遇到了與貓鼬問題(我嚴重依賴)。搜索論壇後,溶液呈thunkify:KoaJS與Thunkify + Mongoose

的CoffeeScript:

Controller = require './basecontroller' 
User = require '../models/user' 

UserController = ((c, User) -> 
    return (
    get: (next) -> 
    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 

的JavaScript:

// 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; 

這是有道理的,我的頭......但我收到此錯誤:

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.<anonymous> (/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.<anonymous> (/Users/mial/Projekte/MiPa/schuppen4-on-heroku/node_modules/koa/node_modules/co/index.js:45:5) 
14:56:01 web.1 |  at Server.<anonymous> (/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) 

...嗯。幫幫我? :)

回答

2

固定它:)

Controller = require './basecontroller' 
User = require '../models/user' 

UserController = ((c, User) -> 
    User.find = c.libs.thunkify User.find 
    return (
    get: (next) -> 
    try 
     users = yield User.find {} 
     @body = users 
    catch e 
     console.error "Error: #{e}" 
) 
) Controller, User 

# Export functionality 
module.exports = UserController 

顯然,我不得不「覆蓋」的User.find功能和我的模塊模式return語句之前定義thunkify。


更新:無需與貓鼬使用thunkify。你可以只使用exec功能,因爲它已經是一個 承諾,這co還與(感謝@ jmar777):

Controller = require './basecontroller' 
User = require '../models/user' 

UserController = ((c, u) -> 
    return (
    get:() -> 
    try 
     users = yield u.find({}).exec() 
     @body = users 
    catch e 
     console.error e 
) 
) Controller, User 

module.exports = UserController 
+1

小的修正:['.exec()'返回一個承諾(HTTP ://mongoosejs.com/docs/api.html#query_Query-exec),其中'co'也適用。很高興你的工作,雖然:) – jmar777 2014-10-04 01:18:00

+0

真棒 - 越來越接近理解所有的發電機魔術:D謝謝@ jmar777! – Michel 2014-10-05 11:29:46