简体   繁体   English

创建两个模型时对sailsjs的错误处理

[英]sailsjs error handling when creating two models

I'm pretty new two nodejs and sails. 我是新的两个nodejs和帆。 I'm trying to create two models inside one action. 我试图在一个动作中创建两个模型。 My question is how to handle the errors which can occur in both queries. 我的问题是如何处理两个查询中可能发生的错误。

Current Code: 当前代码:

new: function (req, res) {
    var errorArray = [];
    var data = req.allParams();

        User.create({
            username: data.username,
            password: data.password,
            device_id: data.device_id,
            own_number: data.own_number,
            mobile_numbers: data.mobile_numbers
        }).exec(function (err, user) {
            if(err){
            errorArray.push(err);
            }
        });

        Mobile_number.create({
          number: data.own_number,
          number_id: this.hash(data.own_number)
        }).exec(function(err, mobile_number){
          sails.log(err);
          if(err){
            errorArray.push(err);
          }
       });

       if(errorArray.length == 0){
         return res.ok('success');
       }else {
        return res.send(errorArray.toString());
       }    
  }

The problem with this code is that the if at the end is handled before the queries finish. 此代码的问题在于,如果在查询结束之前处理了if末尾。 What would be the right way for to wait for the queries? 等待查询的正确方法是什么?

Bruno 布鲁诺

First of all your code will not work because node.js is asynchronous. 首先,由于node.js是异步的,因此您的代码将无法工作。 So you check if there are errors before functions are executed. 因此,在执行功能之前,请检查是否有错误。 Below are 2 solutions: 以下是2个解决方案:

You can use async.series . 您可以使用async.series If you use that it will stop executing if first method fails and it will return it's error. 如果使用它,它将在第一个方法失败时停止执行,并返回错误。 It it succeed it will go to second method. 如果成功,它将转到第二种方法。

async.series([
        function(callback) {
            User.create({
                username: data.username,
                password: data.password,
                device_id: data.device_id,
                own_number: data.own_number,
                mobile_numbers: data.mobile_numbers
            }).exec(callback);
        },
        function(callback) {
            Mobile_number.create({
                number: data.own_number,
                number_id: this.hash(data.own_number)
            }).exec(callback);
        }
    ],
    function(error, results) {
        // error first finished 
        if(error)
            res.send('error');
        else
            res.send('success');
    }
);

Or you can do it traditional way with callbacks. 或者,您也可以使用回调的传统方式。

new: function(req, res) {
    var errorArray = [];
    var data = req.allParams();

    var mobileCreateCallback = function(err, mobile_number, user) {
        sails.log(err);
        if (err) {
            errorArray.push(err);
        }

        if (errorArray.length === 0) {
            sails.log(user, mobile_number);
            return res.ok('success');
        } else {
            return res.send(errorArray.toString());
        }
    };

    var userCreateCallback = function(err, user) {
        if (err) {
            errorArray.push(err);
        }

        Mobile_number.create({
            number: data.own_number,
            number_id: this.hash(data.own_number)
        }).exec(function(error, mobile_number) {
            mobileCreateCallback(error, mobile_number, user);
        });
    };

    User.create({
        username: data.username,
        password: data.password,
        device_id: data.device_id,
        own_number: data.own_number,
        mobile_numbers: data.mobile_numbers
    }).exec(userCreateCallback);
}

You should reed about callbacks: callbackhell and asynchronous functions in node.js/sails.js. 您应该了解一下回调:node.js / sails.js中的callbackhell和异步函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM