在将我的图像作为字符串单独存储在postgres中之后。当我转到路由时,图像看起来是这样的(我目前得到的响应)
"images":"image1.png-2020-11-11T13:15:55.692Z"
我试着把它弄成这样
"images":["url":"image1.png-2020-11-11T13:15:55.692Z_full","thumbnailUrl":"image1.png-2020-
11-11T13:15:55.692Z_thumb"]
这是我的get请求
router.get("/", (req, res) =>
Post.findAll({
order: [["createdAt", "DESC"]],
include: { model: Post_Image, attributes: ["id", "images"] },
}).then((posts) => {
const baseUrl = config.get("assetsBaseUrl");
for (let post of posts) {
const postImages = post.Post_Images;
const img = postImages.map((post) => post.dataValues);
const imgFileName = img.map((post) => post.images);
const IMAGES = imgFileName.map((image) => ({
url: `${baseUrl}${image}_full.jpg`,
thumbnailUrl: `${baseUrl}${image}_thumb.jpg`,
}));
console.log(IMAGES)
}
res.send(posts);
});
});
当我执行console.log(IMAGES)时,下面是我的响应
[
{
url: 'http://LOCALHOST:PORT/assets/image1.png-2020-11-11T13:15:55.692Z_full.jpg',
thumbnailUrl: 'http://LOCALHOST:PORT/assets/image1.png-2020-11-
11T13:15:55.692Z_thumb.jpg'
}
]
我想做的是
return{
...post,
images:IMAGES}
但没有奏效。
以下是我希望在到达路由时从get请求中获得的响应:
"images": [{"url":"http://192.168.1.103:9000/assets/jacket1_full.jpg","thumbnailUrl" :"http://192.168.1.103:9000/assets/jacket1_thumb.jpg"}]
您应该将POST
模型实例转换为普通对象,并向每个对象添加images
prop:
const plainPosts = posts.map(x => x.get({ plain: true }))
for (const post of plainPosts) {
const IMAGES = post.Post_Images.map((postImage) => ({
url: `${baseUrl}${postImage.images}_full.jpg`,
thumbnailUrl: `${baseUrl}${postImage.images}_thumb.jpg`,
}));
console.log(IMAGES)
post.images = IMAGES
}
res.send(plainPosts);