Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js nodejs、postgres、sequelize-不是获取文件名,而是尝试获取完整路径_Node.js_Sequelize.js - Fatal编程技术网

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

Node.js nodejs、postgres、sequelize-不是获取文件名,而是尝试获取完整路径,node.js,sequelize.js,Node.js,Sequelize.js,在postgres中将我的图像单独存储为字符串之后。当我走到路线时,图像看起来像这样(我当前得到的响应) 我正试着这样做 "images":["url":"image1.png-2020-11-11T13:15:55.692Z_full","thumbnailUrl":"image1.png-2020- 11-11T13:15:55.692Z_thumb"] 这是我的请求 router.get

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

我正试着这样做

"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(图像)时,下面是我的回答

[
  {
   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
模型实例转换为普通对象,并向每个实例添加
图像
道具:

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

显示一个应该返回的数据示例我刚刚更新了帖子,请看一看,这是最后一个代码底部的注释,不用于扩展讨论;这段对话已经结束。
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
  });