nodejs、postgres、sequelize - 而不是获取文件名,而是尝试获取完整路径 [英] nodejs, postgres, sequelize - instead of getting the filename, trying to get full path

查看:70
本文介绍了nodejs、postgres、sequelize - 而不是获取文件名,而是尝试获取完整路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将我的图像作为字符串单独存储在 postgres 之后.当我去路线时,图像看起来像这样(我目前得到的响应)

 图像":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({顺序:[[createdAt",DESC"]],包括:{模型:Post_Image,属性:[id",图像"]},}).then((posts) => {const baseUrl = config.get(assetsBaseUrl");for(让帖子发帖){const postImages = post.Post_Images;const img = postImages.map((post) => post.dataValues);const imgFileName = img.map((post) => post.images);const IMAGES = imgFileName.map((image) => ({网址:`${baseUrl}${image}_full.jpg`,缩略图网址:`${baseUrl}${image}_thumb.jpg`,}));控制台日志(图像)}res.send(posts);});});

当我执行 console.log(IMAGES) 时,这是我的回应

<预><代码>[{url: 'http://LOCALHOST:PORT/assets/image1.png-2020-11-11T13:15:55.692Z_full.jpg',缩略图网址:'http://LOCALHOST:PORT/assets/image1.png-2020-11-11T13:15:55.692Z_thumb.jpg'}

]

我正在尝试做

返回{...邮政,图像:图像}

但是没有用.

这是我在去路由时期望从我的 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_Images, ...postAttributes } = postconst IMAGES = Post_Images.map((postImage) => ({网址:`${baseUrl}${postImage.images}_full.jpg`,缩略图网址:`${baseUrl}${postImage.images}_thumb.jpg`,}));控制台日志(图像)resultPosts.push({... postAttributes,图像:图像})}res.send(resultPosts);

如果你只收到一个帖子,那么这应该是这样的:

const plainPost = post.get({ plain: true })const { Post_Images, ...postAttributes } = plainPostconst IMAGES = Post_Images.map((postImage) => ({网址:`${baseUrl}${postImage.images}_full.jpg`,缩略图网址:`${baseUrl}${postImage.images}_thumb.jpg`,}));控制台日志(图像)重新发送({... postAttributes,图像:图像});

After storing my images separately as strings in postgres. when i go to the route, images looks like this (the response i am currently getting)

 "images":"image1.png-2020-11-11T13:15:55.692Z"

i am trying to get it like this

"images":["url":"image1.png-2020-11-11T13:15:55.692Z_full","thumbnailUrl":"image1.png-2020- 
11-11T13:15:55.692Z_thumb"]

Here is my get request

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);
 });
});

when i do console.log(IMAGES), here is my response

[
  {
   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'
  }

]

i was trying to do

return{
...post,
images:IMAGES}

but it did not work.

Here is the response i am expecting to get from my get request when i go to the route:

  "images": [{"url":"http://192.168.1.103:9000/assets/jacket1_full.jpg","thumbnailUrl" :"http://192.168.1.103:9000/assets/jacket1_thumb.jpg"}]

解决方案

You should turn Post model instances into plain objects and add images prop to each of them:

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);

if you get a single post only then this should look like:

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
  });

这篇关于nodejs、postgres、sequelize - 而不是获取文件名,而是尝试获取完整路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆