简体   繁体   English

将promise链中的值传递给处理程序

[英]passing value in promise chain to handlers

Having a problem passing values in promise chain. 在promise链中传递值时出现问题。 I have a CustomerController that talks to CustomerRepo 我有一个与CustomerRepo对话的CustomerController

 var express = require('express')();
 var customerRepo = require('../repositories/customerRepo');

 var customerController = function () {
 var get = function (req, res) {
    if (typeof(req.query.offset) === 'undefined' ||        typeof(req.query.return) === 'undefined')
        res.status(422).send({'message': 'missing paging parameters'});

    req.query.offset === '' ? req.query.offset = 0 : req.query.offset;
    req.query.return === '' ? req.query.return = 50 : req.query.return;

    let getCustomers = customerRepo.getCustomers(req.query.offset, req.query.return);
    let getProfiles = customerRepo.getProfiles(customerList);
    let sendPayLoad= function (customerList) {
        console.log(customerList); /////  <===============
        res.send(customerList);
    }

    getCustomers
        .then(getProfiles)
        .then(sendPayLoad)
        .catch(function (err) {
            res.status(500).send(err);
        })
  }
  return {
     get: get
  }
}
module.exports = customerController;

the two methods in CustomerRepo CustomerRepo中的两个方法

    var getCustomers = function (offset, _return) {
    return new Promise(function (resolve, reject) {
    {
        var customers = [];
        var sql = 'sql here';
        SqlQuery(sql)
            .then(function (rows) {
                for (var i = 0; i < rows.length; i++) {
                    var customer = new Customer();
                    customer = rows[i];
                    customers.push(customer);
                }
                resolve(customers[0]);
            })
            .catch(function (err) {
                reject(err);
            })
    }
    });
}

var getProfiles = function (customers) {
    return new Promise(function (resolve, reject) {
    let ids = customers.map(function (item) {
        return item.CustomerId;
    });
    var customerList = [];
    let sql = 'sql here';
    SqlQuery(sql)
        .then(function (rows) {
            // do some processing to extract profile data from rows 
               and push to customerList so my profiles can be retrieved          like customers[0].Profiles
            console.log(customerList);/////  <===============
            resolve(customerList);
            return customerList;
        })
        .catch(function (err) {
            reject(err);
        })
    });
}

module.exports = {
    getCustomers: getCustomers,
    getProfiles: getProfiles
}

console.log(customerList) in getProfiles produces the required json response I am looking for, like the below getProfiles中的console.log(customerList)生成我正在寻找的所需json响应,如下所示

    [{ "CustomerId" : 123,
   "Name" : "myCustomer",
   .....
   Profiles[{
     "profile1" : "myProfile",
      .....
   }]
]

but my res.send(customerList) in the controller is returning only the customer without the profiles. 但我在控制器中的res.send(customerList)只返回没有配置文件的客户。 the problem is essentially passing values between promises. 问题基本上是在承诺之间传递价值。

sendPayload is not defined. sendPayload未定义。 Your function is named send. 您的函数名为send。 It's passing the data to an undefined function. 它将数据传递给未定义的函数。 That's likely why you're not seeing it. 这可能就是为什么你没有看到它。

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

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