第二级包括使用 sequelize
second level include using sequelize
如果用户可以有很多post,每个post可以有很多图片,这是否意味着用户和图片是关联的?因为目前我正在尝试向 posts 和 posts 的每个用户展示我想使用上述方法将图像显示为 url 和缩略图 url .我不断收到 Post_Image 未关联到用户的错误。
我目前正在尝试此代码,但一直收到无法读取 属性 地图的错误消息。
router.get("/posts", auth, (req, res) => {
let { id } = req.user;
User.findByPk(id, {
include: [
{
model: Post,
include: [{ model: Post_Image, attributes: ["id", "images"] }],
},
],
}).then((post) => {
if (!post)
return res.status(404).send();
const baseUrl = config.get("assetsBaseUrl");
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`,
}));
res.send({ ...postAttributes, images: IMAGES });
});
});
在这种情况下,它类似于在
中处理多个帖子
区别在于在哪里调用get({ plain: true })
:
User.findByPk(id, {
include: [
{
model: Post,
include: [{ model: Post_Image, attributes: ["id", "images"] }],
},
],
}).then((user) => { // we got a user here and not a post or posts
if (!user)
return res.status(404).send();
const baseUrl = config.get("assetsBaseUrl");
const plainUser = user.get({ plain: true })
const resultPosts = []
for (const post of plainUser.Posts) {
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);
如果用户可以有很多post,每个post可以有很多图片,这是否意味着用户和图片是关联的?因为目前我正在尝试向 posts 和 posts 的每个用户展示我想使用上述方法将图像显示为 url 和缩略图 url .我不断收到 Post_Image 未关联到用户的错误。
我目前正在尝试此代码,但一直收到无法读取 属性 地图的错误消息。
router.get("/posts", auth, (req, res) => {
let { id } = req.user;
User.findByPk(id, {
include: [
{
model: Post,
include: [{ model: Post_Image, attributes: ["id", "images"] }],
},
],
}).then((post) => {
if (!post)
return res.status(404).send();
const baseUrl = config.get("assetsBaseUrl");
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`,
}));
res.send({ ...postAttributes, images: IMAGES });
});
});
在这种情况下,它类似于在
区别在于在哪里调用get({ plain: true })
:
User.findByPk(id, {
include: [
{
model: Post,
include: [{ model: Post_Image, attributes: ["id", "images"] }],
},
],
}).then((user) => { // we got a user here and not a post or posts
if (!user)
return res.status(404).send();
const baseUrl = config.get("assetsBaseUrl");
const plainUser = user.get({ plain: true })
const resultPosts = []
for (const post of plainUser.Posts) {
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);