【问题标题】:Nested conditional promises using pg-promise and bluebird使用 pg-promise 和 bluebird 嵌套条件承诺
【发布时间】:2017-01-16 08:26:58
【问题描述】:

我要做的就是运行一个查询,检查一个电子邮件地址是否已经注册,如果是则拒绝请求,如果没有运行一些验证,然后在我的用户表中创建用户。这有点像shell,代码最初基于https://github.com/vitaly-t/pg-promise-demo,那个代码很棒,但我不是。承诺对我来说完全是荒谬的,我不知道我应该返回什么或在哪里。

此代码运行并实现了预期的目标,但随后它遇到问题并发送回“无法读取未定义的属性'then'”。当我尝试链接第二个 .then 以使其不在第一个 .then() 中时,我收到错误,我试图在 res 已发送后更改它。

谁能帮我理解这里出了什么问题以及如何解决它?

useradd: function (req, res) {
    db.users.findbyemail(req.body.email)
        .then(function(data){
            if( typeof data === undefined || data === null) {
                 db.users.add({
                    provider: 'local',
                    email: req.body.email, 
                    password: req.body.password, 
                    salt: 'salt', 
                    displayName: req.body.displayName })
                    .then(function(data){
                        res.json({
                            success: true,
                            data: "Account created"
                        })
                    }) 
            } else {
                 res.json({
                    success: false,
                    error: "Email address " + req.body.email + " is already registered on this site"
                })
            }
        })    

        .catch(function(error){
            res.json({
                success: false,
                error: error.message || error
            })
        })
}

【问题讨论】:

  • "我不知道" - 你可能想看看my rules of thumb
  • 如果您在捕获中收到错误“无法读取属性 'then' of undefined”,这表明 db.users.add 没有返回承诺 - 对我来说,这听起来像是一个错误图书馆。
  • 只有当您的方法 users.add 未能返回承诺时才会发生这种情况。 @Bergi LOL,你在我之前发布了几秒钟 :) 虽然它与库无关,但方法 users.add 是他自己的代码;)
  • 此外,调用db.users.add 而不是return db.users.add 将导致代码没有.catch
  • @user2338041 你的问题就在那里,你没有从方法中返回任何东西。您现在是否在认真地问我们如何从函数中返回值? :)

标签: node.js express promise bluebird pg-promise


【解决方案1】:

我最终得到了一些工作,应用 Bergi 的规则确实将我推向了正确的方向,我还决定重构以使用 throws,因此只有一个成功路径,所有其他路径都使用 .catch 从而避免在发送后更改 res问题。主代码现在如下所示:

useradd: function (req, res) {
    return db.users.findbyemail(req.body.email)
       .then(function(data){
           return module.exports.validateUser(data, req);
        })
        .then(function(){
            return db.users.add({
                provider: 'local',
                email: req.body.email, 
                password: req.body.password, 
                salt: 'salt', 
                displayName: req.body.displayName })  
        })
        .then(function(){
            res.json({
                success: true,
                data: "Account created"
            })
            return "acct created";
        }) 
        .catch(function(error){
            res.json({
                success: false,
                error: error.message || error
            })
        })
},
validateUser: function(data, req) {
    console.log("got to validateUser, data " + data + " req " + req);
    if (typeof data !== 'undefined' && data !== null) {
        throw new Error("Email address " + req.body.email + " is already registered on this site");
    } else {
        //validation goes here
        return null;
}
},

//with the db.users.add function modified from the version in my comment to:
add: (values)  => {    
        var reponeres = rep.one(sql.add, values);
        return reponeres; 
    },

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2016-08-20
  • 2017-07-18
  • 2016-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-18
  • 2018-02-01
相关资源
最近更新 更多