我正在尝试弄清楚如何在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);发布于 2020-09-30 02:01:19
这段代码运行良好。attribute关键字现在起作用,并且只返回一个字段。这似乎是git的一个问题。
https://stackoverflow.com/questions/64111043
复制相似问题