如何使用节点js获取特定值

How to get specific value using node js

如何从 findAll() 返回的对象中提取 属性 name 因为我想要我的数据库中存在的所有名称但得到 undefined

function checkDoc(childProduct) {
  return new Promise((resolve, reject) => {
    Document.findAll({
      raw:true,
      where: {
        product_id: childProduct.id,
      },
    })
      .then((productDoc) => {
        console.log(productDoc);
      })
      .catch(function (err) {
        return reject(
          getFailureResponseJson("Can't be added please try again :) " + err)
        );
      });
  });
}

输出自:console.log(product);

{
id: 01,
name: ABC,
description: demo,
}
{
id: 02,
name: PQR,
description: demo,
}
{
id: 03,
name: XYZ,
description: demo,
}

我正在做 console.log(product.name) 但它显示 undefined - 我怎样才能得到这个名字?

尝试对产品使用 foreach 循环:

productDoc.forEach((singleProduct) => {
   console.log('Product Name: ', singleProduct.name)
});

或者简单地说:

console.log('Product Name: ', productDoc[0].name)
// where '0' is the index of first product

您可能会得到一组对象,而不是单个对象。