简体   繁体   English

如何在Node.js中创建有效的跟随者-跟随者机制

[英]How to create efficient following - follower mechanism in Node.js

I have a Node.js application, with a simple follow mechanism (which allows a user to get the latest articles for those he/she follows). 我有一个Node.js应用程序,它具有简单的关注机制(允许用户获取他/她关注的对象的最新文章)。

I implemented this, I creating an array called followers, containing the userIDs of all users following a particular user. 我实现了这一点,创建了一个名为Followers的数组,其中包含跟随特定用户的所有用户的userID。 To get the news feed, I simply forEach in every user's followers array, to find which of them contains logged in user's id. 为了获得新闻提要,我只是在每个用户的关注者数组中分别对forEach进行查找,以找出其中的哪个包含已登录用户的ID。 If it is found, all the posts of those users should be retrieved in JSON format. 如果找到,则应以JSON格式检索这些用户的所有帖子。 This works for now, but I believe as many more users sign up, fetching the feed will become a more cumbersome task, slowing down the feed page's loading. 目前,此操作有效,但我相信,随着越来越多的用户注册,获取提要将变得更加繁琐,从而减慢了提要页面的加载速度。

What is the best way to correct this problem, and structure the following mechanism? 解决此问题并构造以下机制的最佳方法是什么?

NB: My app uses Express, Mongoose and PUG for templating. 注意:我的应用程序使用Express,猫鼬和PUG进行模板制作。

You need to create a user model. 您需要创建一个用户模型。 And if you are using mongodb, you need to use mongoose.Schema. 如果使用mongodb,则需要使用mongoose.Schema。 First you have to npm install mongoose. 首先,您必须npm安装猫鼬。 And then you need to create your user object like this: 然后,您需要像这样创建用户对象:

 const mongoose = require("mongoose"); var UserSchema = mongoose.Schema({ username:{ type: String, //unique: true, trim: true, minLength: 1, required: true }, name:{ type: String, // unique: true, trim: true, minLength: 1, required: true }, email:{ type: String, //unique: true, trim: true, minLength: 1, required: true, validate:{ validator: validator.isEmail, message: '{VALUE} is not a valid email' } }, password:{ type: String, required: true, minlength: 4 } }); 

I suggest you look for some MEAN app tutorials for further help. 我建议您寻找一些MEAN应用程序教程以获得进一步的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM