【问题标题】:Convert callback to thunk将回调转换为 thunk
【发布时间】:2014-03-20 14:33:34
【问题描述】:

我在 koa.js 中使用猫鼬(可能是一个糟糕的选择,但必须坚持下去)。 我最初的回调函数是:

 var _project = yield parse(this);
var userdetails = this.req.user;
var that = this ;
//=============================================================
//FInd a user , check the project name exists under the user, if not then create one
//=============================================================
User.findOne({ '_id': userdetails._id }, function (err, user) {
    if (err) {
        this.body = "please login again , your session seems to have expired"
    } console.log(user.projects.owner.indexOf(_project.name));
    if(user.projects.owner.indexOf(_project.name) == -1) { //This means the project is not yet created
        var temp_project = new Project(_project);
            temp_project.save(function save() {
            if(err) {
                that.body = "Project coudn't be saved, Please try again sometime later";
            } else {
                user.projects.owner.push(_project.name);
                user.save(function save() {
                if (err) {
                    that.body = "This error is highly unlikely, yet if you see this .Please report this issue";
                }
                });
                that.body = temp_project;
            }
            });
    }
     if(user.projects.owner.indexOf(_project.name) >= 0) { //THis means the project exists
        that.body = "You have already created a project with same name, please use a different name";
         console.log("you reached till here");
    }
});

这应该在正常的快递世界中工作,但后来我意识到我需要以 thunk 的形式重写,所以我目前的尝试是

function userfindONE(err, user) {
    if (err) {
        return "please login again , your session seems to have expired"
    } 
    if(user.projects.owner.indexOf(tproject.name) == -1) { //This means the project is not yet created
        var temp_project = new Project(tproject);
            temp_project.save(function save() {
            if(err) {
                return "Project coudn't be saved, Please try again sometime later";
            } else {
                user.projects.owner.push(tproject.name);
                user.save(function save() {
                if (err) {
                    return "This error is highly unlikely, yet if you see this .Please report this issue";
                }
                });
                return temp_project;
            }
            });
    }
     if(user.projects.owner.indexOf(tproject.name) >= 0) { //THis means the project exists
        return "You have already created a project with same name, please use a different name";
    } else return "nothing is matching";
}

function userfindone(userdetails) {
return function(cb) {
    User.findOne({ '_id': userdetails._id }, cb);
  };
}
 var userdetails = this.req.user;
var tproject = yield parse(this);

但这会从第一个 mongoose 调用的 User.findone 中返回用户详细信息。 其他任何事情似乎都被忽略了。谢谢

this.body = yield userfindone(userdetails)(userfindONE) ;

【问题讨论】:

    标签: node.js generator thunk koa


    【解决方案1】:

    看看node-thunkify。它应该像用它包装架构的函数一样简单。

    【讨论】:

      【解决方案2】:

      使用 Mongoose 3.9.x,您可以简单地 yield user.save(),检查您的 package.json 您是否安装了不稳定版本。

      【讨论】:

        猜你喜欢
        • 2014-10-23
        • 1970-01-01
        • 1970-01-01
        • 2018-04-23
        • 2019-04-27
        • 2018-05-04
        • 1970-01-01
        • 1970-01-01
        • 2017-03-22
        相关资源
        最近更新 更多