处理req res和node express中的错误

时间:2018-06-12 17:30:11

标签: javascript node.js http express frameworks

这是一个理论问题。

我正在构建一个MEAN堆栈应用程序,我正在尝试理解处理请求和响应周期的最佳方法。

首发: 我的应用程序正在与AWS,Quickbooks(会计应用程序)和Shopify(电子商务应用程序)等服务器API进行通信。 为了更好地说明,当应用程序创建/更新/删除/从客户/产品模型获取时,它会检查会计和电子商务网关的状态。如果为任一网关激活模型,则根据req执行put / post / get操作。 然后它计算响应,由于进程太多,通常需要更长的时间

这是一项更新操作。

router.route("/:model/:model_id")
.put(jsonParser, function(req, res) {
    validatePermission(req, res, function() { // validate if user is allowed ro perform this operation or not.
        var Model = getModel(req, res); //get customer/ product model

        if (Model) {
            Model.findById(req.params.model_id, function(err, model) {
                if (err) {
                    res.send(err);
                    return;
                }
                var filledModel = {};
                var pendingCallBacks = [];
                if (model._doc) {
                    /**
                     * neccessary changes on the model obj
                    */
                    Model.findByIdAndUpdate({ _id: req.params.model_id }, { $set: updateObj }, { runValidators: true, upsert: false, new: true, lean: true }, function(err, raw) {
                        if (err) {
                            res.json({ error: err.message });
                            return;
                        }
                        else {
                            /**
                             * from here is got complicated.
                             * I used the async based as per my understanding of req/res cycle but open for better ways
                             */
                            */                                
                            async.waterfall([
                                checkAccountingGateway, //check for accounting gateway if it is active for the model (customer/product)
                                checkEcommerceGateway, //check for accounting gateway if it is active for the model (product)
                            ], function(err, resObj) {
                                if (err) {
                                    res.status(409).send({ message: err.message });
                                    return;

                                } else {
                                    res.json(returnSuccessObj);
                                    return;
                                }
                            });

                            function checkAccountingGateway(callback) {
                                if (_.isObject(accountingGateway) && _.isFunction(accountingGateway.getActiveConnector)) {

                                    accountingGateway.getActiveConnector(req, function(connector) {
                                        if (_.isObject(connector) && connector.isActive()) {
                                            if (connector.isAccountingModel(req.params.model)) { // to check if the model is required to sync with accounting system
                                               /**
                                                * calculate accounting_id 
                                                */
                                                //if accounting_id is defined just update 
                                                if (accounting_id !== null) {
                                                    //to update a record on QB we need ID (accountind_id) and sync token
                                                    connector.putItem(req, req.params.model, accounting_id, raw, function(err, updatedItem) {
                                                        if (err || updatedItem === null) {
                                                            callback(err, returnObj);

                                                        } else {
                                                            Model.findById({ _id: req.params.model_id }, function(err, updatedModel) {
                                                                updatedModel = updatedModel._doc || updatedModel;
                                                                if (err || updatedModel === null) {
                                                                    callback(err, returnObj);

                                                                } else {
                                                                    returnSuccessObj = updatedModel;
                                                                    callback(null, returnObj);
                                                                }
                                                            });
                                                        }
                                                    });

                                                } else { //if accountind_id is not defined then create a new model
                                                    connector.postItem(req, req.params.model, raw, function(err, accountingId) { //item with accounting ID
                                                        if (err || accountingId === null) {
                                                            callback(err, returnObj);
                                                        } else {
                                                            // inserting accounting_ids info to the target collection
                                                            var newAccountingId = {
                                                                connector_id: connector.data.type._id,
                                                                id: accountingId
                                                            };
                                                            Model.findByIdAndUpdate({ _id: req.params.model_id }, { $addToSet: { accounting_ids: newAccountingId } }, { runValidators: true }, function(err, raw) {
                                                                if (err) {
                                                                    returnObj.push("error fetching record after postItem function;");
                                                                    callback(err, returnObj);
                                                                } else {
                                                                    returnObj.push("Accounting system postItem ends with a success");
                                                                    returnSuccessObj = raw;
                                                                    callback(err, returnObj);
                                                                }
                                                            });

                                                        }
                                                    });
                                                }
                                            } else {
                                                callback(null, returnObj);
                                            }

                                        } else {
                                            callback(null, returnObj);
                                        }

                                    });
                                } else {
                                    returnObj.push("Accounting connector does not exists");
                                    callback(null, returnObj);
                                }
                            }

                            function checkEcommerceGateway(returnObj, callback) {
                                /**
                                 * almost similar code for ecommerce
                                 */
                            }


                        }
                    });
                }
            });
        }
    });
})

`

所以我的问题是:

  1. 我应该等待响应并仅在收到时发送 更新/计算但网关?

  2. 有没有办法可以发送 更新我的本地数据库后回复,然后发送一些吐司 消息或quickbooks / shopify更新的消息......

0 个答案:

没有答案