gpt4 book ai didi

sequelize.js - 理解如何解析/传递 sequelize 查找结果的问题

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

我想弄清楚如何在 sequelize 搜索查询中获取特定属性。我试过使用属性选项,但这似乎并不像我期望的那样工作......


async function getImagesByPetName(petname) {
const images = await Images.findAll({
attributes: ['Location'],
where: {
Petname: petname.toLowerCase(),
},
});
return images;
//return JSON.stringify(images,
}

它确实过滤正确,我得到了这些结果:

Images are : [
{
"id": 6,
"Petname": "archie",
"Name": "hulk-onyx-archie.jpg",
"Location": "https://snootly.s3.amazonaws.com/hulk-onyx-archie.jpg",
"createdAt": "2020-09-28T12:04:24.000Z",
"updatedAt": "2020-09-28T12:04:24.000Z",
"PetId": null
},
{
"id": 7,
"Petname": "archie",
"Name": "archie-older.jpg",
"Location": "https://snootly.s3.amazonaws.com/archie-older.jpg",
"createdAt": "2020-09-28T12:04:25.000Z",
"updatedAt": "2020-09-28T12:04:25.000Z",
"PetId": null
}
]
我的主要问题是如何访问每个图像数组的位置...
app.get("/getimages", (req, res) => {
var id = req.query.id;
db.getImagesByPetName(id).then((images) => {
//loop through the image record and pull out the url
//var imagelist = [];
// for (i = 0; i < images.length; i++) {
console.log("The location is: " + images[12]);
// imagelist.push(images[i].Location);
//}
console.log("Images are : " + images);

res.send(images);
});
});

这是我设置数据库模型的方式:
onst Pets = sequelize.define(
"Pets",
{
name: {
type: Sequelize.STRING,
unique: true,
},
image: {
type: Sequelize.STRING,
unique: false,
},

},
{}
);

const Images = sequelize.define(
"Images",
{
Petname: {
type: Sequelize.STRING,
unique: false,
},
Name: {
type: Sequelize.STRING,
unique: true,
},
Location: {
type: Sequelize.STRING,
unique: true,
},
},
{}
);

Images.belongsTo(Pets);

最佳答案

这段代码工作正常。属性关键字现在正在工作并只返回一个字段。这似乎是 git 的问题。

关于sequelize.js - 理解如何解析/传递 sequelize 查找结果的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64111043/

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