为什么请求帖子未运行

时间:2018-09-07 20:51:29

标签: javascript node.js

我当前遇到request.post()未运行的一些问题。我的代码以某种方式跳过了if和else语句中的request.post()。谁能告诉我我做错了什么或如何改进我的代码?如果有人也向我解释我做错了,那将是很好的。

CurrentOrder.findOneAndUpdate(_id, orderBody, function (err, data) {
            if (err) return next(err)

            CurrentOrder.findOne(_id2, function(err, order) {
                if (err) return next(err)

                if(order.canadaTrackingStatus.length == 1 && orderBody.paymentStatus == 'paid'){
                    order.canadaTrackingStatus = [order.canadaTrackingStatus[0], {date: new Date(), status: '门店已揽收'}];
                    order.save(function(err) {
                        if (err) return next(err)
                        var modItems = merge_items(order.items)
                        var url = {OrderCode:order.orderNumericCode,Sender:{Name:order.senderName,Tel:order.senderPhone,ProvinceName:order.senderProvince,CityName:order.senderCity,ExpAreaName:order.senderCity,Address:order.senderAddress},Receiver:{Name:order.recipientName,Tel:order.recipientPhone,ProvinceName:order.recipientProvince,CityName:order.recipientCity,ExpAreaName:order.recipientCity,Address:order.recipientAddress},Content:modItems}
                        var newurl = encodeURI(JSON.stringify(url))
                        console.log(newurl)
                        request.post({
                            headers: { 'Content-Type':'content=text/html;charset=utf-8' },
                            url : `http://rrr.cn/order/create?EBusinessID=1afs190&AppKey=trytr5fasfasf68980hg23gff&Sign=a1a4e30c83c8c9e6d6fd1fasfasfac55dbc1f2f&RequestType=100232&RequestData=${newurl}`
                        },function(error,response,body){
                            var chinacode = JSON.parse(body).logisticcode
                            var newbody = Object.assign(order, {vipOrderNumericCode : chinacode})
                            CurrentOrder.findOneAndUpdate({_id:order._id}, newbody, function (err, data2) {
                                if(err) return next(err)
                                console.log('if')
                            })
                        })
                    });
                }else if(order.canadaTrackingStatus.length == 1 && orderBody.paymentStatus == 'pending'){
                    order.canadaTrackingStatus = [order.canadaTrackingStatus[0]];
                    order.save(function(err) {
                        if (err) return next(err)
                        var modItems = merge_items(order.items)
                        var url = {OrderCode:order.orderNumericCode,Sender:{Name:order.senderName,Tel:order.senderPhone,ProvinceName:order.senderProvince,CityName:order.senderCity,ExpAreaName:order.senderCity,Address:order.senderAddress},Receiver:{Name:order.recipientName,Tel:order.recipientPhone,ProvinceName:order.recipientProvince,CityName:order.recipientCity,ExpAreaName:order.recipientCity,Address:order.recipientAddress},Content:modItems}
                        var newurl = encodeURI(JSON.stringify(url))
                        console.log(newurl)
                        request.post({
                            headers: { 'Content-Type':'content=text/html;charset=utf-8' },
                            url : `http://rrr.cn/order/create?EBusinessID=11987650&AppKey=trytr56809876980hg23gff&Sign=a1a4e30c83c8c9e6d6fd10987ac55dbc1f2f&RequestType=1002&RequestData=${newurl}`
                        },function(error,response,body){
                            var chinacode = JSON.parse(body).logisticcode
                            var newbody = Object.assign(order, {vipOrderNumericCode : chinacode})
                            CurrentOrder.findOneAndUpdate({_id:order._id}, newbody, function (err, data2) {
                                if(err) return next(err)
                                console.log('else')
                            })
                        })
                    });
                }
            });

            if (req.user.userRole == 'admin') {
                if (data.batch) res.redirect('/admin/view-batches')
                else res.redirect('/admin/orders')
            } else {
                return res.redirect('/client/current')
            }
        })

0 个答案:

没有答案