我有一个模型(Product),它有一个图像列表。如何获取文件的路径?
const data = await Category
.forge()
.orderBy('order', 'DESC')
.fetchAll({
withRelated: [
'products',
'products.images',
]
})使用上面的查询,我得到了这个'images‘数据:
[{
"id": 6,
"upload_file_id": 6,
"related_id": 2,
"related_type": "products",
"field": "images"
}]Strapi v3.0.0-alpha.19,ORM BookshelfJS (afaik)
发布于 2019-01-25 03:44:06
到目前为止,我能找到的最好的方法是使用upload插件来获取数据:
for (const category of categories) {
for (const product of category.products) {
const files = await strapi.plugins.upload.models.file
.where('id', 'IN', product.images.map(i => i.upload_file_id))
.fetchAll();
product.images = files.models.map(f => f.attributes)
}
}我希望有一个更好的方法来一次获取所有数据
发布于 2021-04-26 23:48:48
您还可以使用upload (媒体库) API端点从Strapi获取数据。
curl http://mystrapidomain.com/upload/files将生成所有已上传资产的列表,以及为替代设备自动生成的格式(如果已启用)。它是一个图像对象数组,其中每个对象都有名称、alt文本、标题、大小和格式以及许多其他信息。
发布于 2021-12-27 19:15:50
您可以使用id通过upload_file模型进行查询,获取所需的数据。
const Data = await strapi.query('file', 'upload').findOne({id : data.upload_file_id});
console.log(Data.url)https://stackoverflow.com/questions/54352206
复制相似问题