承诺nodejs中的每个迭代错误

时间:2015-09-29 01:56:03

标签: node.js iteration promise sails.js bluebird

我正在尝试为我现有的使用sails.js的项目采用bluebird Promise,这是片段:

    var isUserTeamCaptain = function(userId, teamId) {
        return new Promise(function(resolve, reject) {
            Team.findOne({id: teamId}).then(function(team) {
                if (team) {
                    if (userId != team.captain) {
                        reject(sails.__('you are not captain, thus cannot edit team info'));
                    } else {
                        resolve(team);
                    }
                } else {
                    reject(sails.__('team not found'));
                }
            }).catch(function(err) {
                reject(err);
            });
        });
    };
    isUserTeamCaptain('5604145aa8944407362d6abf', '560496d157f6995702b3f931').then(
        function resolve(team) {
            console.log('passed');
            return Promise.resolve(User.find());
        },
        function reject(err) {
            console.log('failed');
            console.log('the error: ' + err);
            throw new Error(err);
        }
    ).each(function(user) {
        console.log(user.username);
    }).then(function() {
        console.log('we are done with each iteration')
    }).catch(function(err) {
        console.log(err.message);
    })

效果很好,每个用户名都可以在控制台中打印,所以'我们可以完成每次迭代',也按照我预期的顺序。

但是,如果我把isUserTeamCaptain放在另一个js文件中,说'TeamService.js',在这里导出并注入TeamService,然后重构上面的代码片段,如下所示:

    TeamService.isUserTeamCaptain('5604145aa8944407362d6abf', '560496d157f6995702b3f931').then(
        function resolve(team) {
            console.log('passed');
            return Promise.resolve(User.find());
        },
        function reject(err) {
            console.log('failed');
            console.log('the error: ' + err);
            throw new Error(err);
        }
    ).each(function(user) {
        console.log(user.username);
    }).then(function() {
        console.log('we are done with each iteration')
    }).catch(function(err) {
        console.log(err.message);
    })

抱怨

.each(function(user) { ...

是一个未定义的功能。我在这里错过了什么吗?

更新:这是TeamService.js:

// TeamService.js - in api/services
var TeamService = module.exports = {
    /**
     * Promise version of isUserCaptainOfTeam
     */
    isUserTeamCaptain: function(userId, teamId) {
        return new Promise(function(resolve, reject) {
            Team.findOne({id: teamId}).then(function(team) {
                if (team) {
                    if (userId != team.captain) {
                        reject(sails.__('you are not captain, thus cannot edit team info'));
                    } else {
                        resolve(team);
                    }
                } else {
                    reject(sails.__('team not found'));
                }
            }).catch(function(err) {
                reject(err);
            });
        });
    }
};

更新:有点发现我的问题所在,我错过了

var Promise = require('bluebird');
TeamService.js中的

......

0 个答案:

没有答案