gpt4 book ai didi

node.js - KoaJS 与 Thunkify + Mongoose

转载 作者:太空宇宙 更新时间:2023-11-03 23:39:41 26 4
gpt4 key购买 nike

我正在开发我的第一个 koajs 项目,在理解(或尝试)生成器函数、yield、thunk 等之后,我遇到了 mongoose(我严重依赖它)的问题。搜索论坛后,解决方案是thunkify:

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)

...嗯。帮助? :)

最佳答案

已修复:)

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 与 mongoose 一起使用。您可以只使用 exec 函数,因为它已经是一个 thunk promise ,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

关于node.js - KoaJS 与 Thunkify + Mongoose,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26179565/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com