[ERR_INVALID_ARG_TYPE]:“ id”参数必须为字符串类型。收到的类型对象

时间:2020-02-21 01:08:59

标签: javascript mysql

通过这种方法,我得到了一个错误:

“ id”参数必须为字符串类型。收到类型对象

我不明白这里出了什么问题。

const customers = await Customer.findAll({
  where: { id: _.map(users, 'customerId') }
});

if (customers) {
  const status = req.query.activation
    ? await Customer.findAll({ where: { activation: req.query.activation } })
    : customers;
  const search = req.query.search
    ? await Customer.findAll({
        name: { where: { [Op.like]: '%' + req.query.search + '%' } }
      })
    : status;

  return res.json({ search });
}
return response.sendBadRequest(res, 'customers empty!');

1 个答案:

答案 0 :(得分:0)

您的ID是字符串,我认为我们不需要查看您的模型

但是_.map生成的数组不是字符串

 map_.map(list, iteratee, [context]) Alias: collect
Produces a new array of values by mapping each value in list through a transformation function (iteratee). The iteratee is passed three arguments: the value, then the index (or key) of the iteration, and finally a reference to the entire list.

_.map([1, 2, 3], function(num){ return num * 3; });
=> [3, 6, 9]

Undeerscorejs Homepage

所以你可以做的就是使用join来获取你的字符串

const customers = await Customer.findAll({ where: {id: _.map(users, 'customerId' ).join('') }})