gpt4 book ai didi

node.js - nodejs、postgres、sequelize - 而不是获取文件名,而是尝试获取完整路径

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

在将我的图像作为字符串单独存储在 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"]
这是我的获取请求
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 属性添加到每个对象中:

const plainPosts = posts.map(x => x.get({ plain: true }))
const resultPosts = []
for (const post of plainPosts) {
const { Post_Images, ...postAttributes } = post
const IMAGES = Post_Images.map((postImage) => ({
url: `${baseUrl}${postImage.images}_full.jpg`,
thumbnailUrl: `${baseUrl}${postImage.images}_thumb.jpg`,
}));
console.log(IMAGES)
resultPosts.push({
...postAttributes,
images: IMAGES
})
}

res.send(resultPosts);
如果你只收到一个帖子,那么这应该是这样的:
const plainPost = post.get({ plain: true })
const { Post_Images, ...postAttributes } = plainPost
const IMAGES = Post_Images.map((postImage) => ({
url: `${baseUrl}${postImage.images}_full.jpg`,
thumbnailUrl: `${baseUrl}${postImage.images}_thumb.jpg`,
}));
console.log(IMAGES)

res.send({
...postAttributes,
images: IMAGES
});

关于node.js - nodejs、postgres、sequelize - 而不是获取文件名,而是尝试获取完整路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64788409/

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