续集:如何从用户ID列表中查询帖子列表?

发布于 2025-01-24 14:25:52 字数 292 浏览 3 评论 0原文

要了解后端应用程序的工作原理,我当前使用nodejs创建了一个类似Instagram的小应用程序并续集(Forn的ReactJs)。

我有3个SQL表:

用户 ID(PK) 用户名 电子邮件 密码

帖子 ID(PK) 用户ID(FK) 信息 MediaUrl

遵循 ID(PK) 用户ID(FK) 后面的ID(FK)

我想知道从与用户ID相对应的一系列以下ID的帖子检索帖子的最清洁方法是什么?

To learn a little bit how a back end application works i'm currently creating a small instagram-like app using nodejs and sequelize (reactjs for the front).

I have 3 SQL tables :

Users
id (pk)
username
email
password

Posts
id (pk)
userId (fk)
message
mediaUrl

Following
id (pk)
userId (fk)
followingId (fk)

I would like to know what is the cleanest way to retrieve posts from an array of followingId corresponding to a userId ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

魔法唧唧 2025-01-31 14:25:52

我只是找到了实现自己想要的东西的方法:

const db = require("../models");
const User = db.users;
const Post = db.posts;
const Following = db.followers;
const Op = db.Sequelize.Op;

exports.requestPost = async (req, res) => {

  const followers = await Following.findAll({
    where: {
      userId: req.body.userId
    },
    attributes: [
      ['followingId', 'followingId']
    ]
  });

  const result = await followers.map(x => x.followingId);

  const posts = await Post.findAll({
    where: {
      userId: result
    }
  });
  return res.send(posts)
}

I just found a way to achieve what i wanted :

const db = require("../models");
const User = db.users;
const Post = db.posts;
const Following = db.followers;
const Op = db.Sequelize.Op;

exports.requestPost = async (req, res) => {

  const followers = await Following.findAll({
    where: {
      userId: req.body.userId
    },
    attributes: [
      ['followingId', 'followingId']
    ]
  });

  const result = await followers.map(x => x.followingId);

  const posts = await Post.findAll({
    where: {
      userId: result
    }
  });
  return res.send(posts)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文