gpt4 book ai didi

node.js - 如何使用 Node js获取特定值

转载 作者:行者123 更新时间:2023-12-03 22:24:22 25 4
gpt4 key购买 nike

如何从name返回的对象中提取属性findAll()因为我想要所有存在于我的数据库中但获得 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
您可能会得到一组对象,而不是单个对象。

关于node.js - 如何使用 Node js获取特定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67177825/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com