Promise.map没有返回值

时间:2017-02-10 07:45:34

标签: javascript node.js promise bluebird

我是Promise的新手

这是我的代码

compileChatnameArray(company_id, paramArr) {
    const Model = this;
    let userObjIdArr = [];

    return Promise.map(paramArr, function (paramObj) {
        const chat_name = paramObj.customer_id;
        const full_name = paramObj.firstname + paramObj.lastname;

        let queryObj = Model.findOne({
                chat_name: chat_name
            })
            .lean()
            .select('_id')
            .exec()
            .then((user) = > {
                if (user) {
                    userObjIdArr.push(user._id);
                    return userObjIdArr;
                } else {
                    var bodyParam = {
                        _company_id: company_id,
                        chat_name: chat_name,
                        full_name: full_name
                    };
                    Model.add(bodyParam)
                        .then((newUser) = > {
                            userObjIdArr.push(newUser._id);
                            return userObjIdArr;
                        })
                }
            });
    })
    .then((userObjIdArr) = > {
        debug(userObjIdArr);
        return Promise.resolve(userObjIdArr);
    })
}

问题是debug(userObjIdArr);

为我打印[ undefined, undefined, undefined, undefined, undefined ]。 (我的数组有5个对象)

//编辑部分 我在map函数中添加了Promise resolve部分,它给出了有线结果。我可以理解,我只是在一个简短的逻辑背后,但无法弄清楚在哪里。

compileChatnameArray(company_id, paramArr) {

  const Model = this;
  let userObjIdArr = [];

  return Promise.map(paramArr, function(paramObj) {

      const chat_name = paramObj.customer_id;
      const full_name = paramObj.firstname + paramObj.lastname;

      let queryObj = Model.findOne({ chat_name: chat_name})
                          .lean()
                          .select('_id')
                          .exec();


      return new Promise(function(resolve, reject) {
          // code starts here
          queryObj.then((user) => {

              if(user) {
                  userObjIdArr.push(user._id)
                  resolve(userObjIdArr);
              } else {
                  //add user here
                    var bodyParam = {
                        _company_id : company_id,
                        chat_name : chat_name,
                        full_name : full_name
                    };
                    Model.add(bodyParam)
                         .then((newUser) => {
                             userObjIdArr.push(newUser._id)
                             resolve(userObjIdArr);
                         })
                         .catch((err) => {
                             reject(err);
                         });

              }
          });
          queryObj.catch((err) => {
              reject(err);
          })
      })
  });

}

现在我得到以下结果

[ [ "589d598f07e32926b4ae9d6b", "589d598f07e32926b4ae9d6c", "589d598f07e32926b4ae9d6e", "589d598f07e32926b4ae9d6f", "589d598f07e32926b4ae9d6d" ], [ "589d598f07e32926b4ae9d6b", "589d598f07e32926b4ae9d6c", "589d598f07e32926b4ae9d6e", "589d598f07e32926b4ae9d6f", "589d598f07e32926b4ae9d6d" ], [ "589d598f07e32926b4ae9d6b", "589d598f07e32926b4ae9d6c", "589d598f07e32926b4ae9d6e", "589d598f07e32926b4ae9d6f", "589d598f07e32926b4ae9d6d" ], [ "589d598f07e32926b4ae9d6b", "589d598f07e32926b4ae9d6c", "589d598f07e32926b4ae9d6e", "589d598f07e32926b4ae9d6f", "589d598f07e32926b4ae9d6d" ], [ "589d598f07e32926b4ae9d6b", "589d598f07e32926b4ae9d6c", "589d598f07e32926b4ae9d6e", "589d598f07e32926b4ae9d6f", "589d598f07e32926b4ae9d6d" ] ]

而我只需要

[ "589d598f07e32926b4ae9d6b", "589d598f07e32926b4ae9d6c", "589d598f07e32926b4ae9d6e", "589d598f07e32926b4ae9d6f", "589d598f07e32926b4ae9d6d" ]

1 个答案:

答案 0 :(得分:0)

这就是我要这样做的方式;

使用内置的promises而不是bluebird和许多微小的功能:

function compileChatnameArray(company_id, paramArr) {
  findOrCreateUsers(company_id, paramArr).then((userObjIdArr) => {
    debug(userObjIdArr)
    // here you are
  })
}

/**
 * @return {Promise<Array<string>>}
 */
function findOrCreateUsers(company_id, paramArr) {
  const tasks = paramArr.map((paramObj) => findOrCreateUser(company_id, paramObj))
  return Promise.all(tasks) // return when ALL tasks are completed
}

/**
 * @param {String} _company_id
 * @param {{customer_id:string, firstname:string, lastname:string}} paramObj
 * @return {Promise<String>}
 */
function findOrCreateUser(_company_id, paramObj) {
  const chat_name = paramObj.customer_id;
  const full_name = paramObj.firstname + paramObj.lastname;
  return Model.findOne({ chat_name }).then((user) => {
    if (user) {
      return Promise.resolve(user._id)
    }
    return createUser({ _company_id chat_name, full_name })
  })
}

/**
 * @param {{_company_id:string, chat_name:string, full_name:string}} userData
 * @return {Promise<String>}
 */
function createUser(userData) {
  return Model.add(userData).then((user) => Promise.resolve(user._id))
}