当节点后端(MongoDB)中的状态更改时通知Angular前端 [英] Inform Angular frontend when state in Node backend (MongoDB) changes

查看:79
本文介绍了当节点后端(MongoDB)中的状态更改时通知Angular前端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在MongoDB中状态更改时实时更新Angular前端.我有什么选择?除例外,还有其他方法吗?使用socket.io?

场景-用户创建提醒,设置电子邮件到达其邮箱的日期.现在,在添加和删除提醒时,我可以在前端侧进行实时模拟,因为我知道用户何时调用导致状态更改的某些操作,但是每个提醒旁边都有一个文本:电子邮件已发送"或电子邮件尚未发送".因为我使用cron作业在后端发送电子邮件,所以前端无法了解.

这是我的第一个项目,我在其中编写后端代码,所以我想知道也许除了实现大量冗余内容只是为了使一个小功能正常工作以外,还有其他方法吗?

NodeJs + Express + Angular6 + MongoDB

解决方案

检查更改流 MongoDB 3.6的最新和最酷的功能,用于从数据库访问实时更改.

示例Node.js代码:

  const collection = db.collection('example');const changeStream = collection.watch();const next =等待changeStream.next(); 

I want a real-time update on Angular frontend when state changes in MongoDB. What are my choices? Are there any other ways than, for ex. using socket.io?

Scenario - user creates a reminder, sets a date on which email will arrive in his mail box. Now for adding and removing reminders I can mimic real-time on front end side, because I know when user invokes some action that leads to a state change, but I also have a text next to every reminder - "Email was sent" or "Email not sent yet". Because I use cron jobs to send emails on my backend, front end cant know about it.

Its my first project where I code my backend, so I just wonder maybe there are some other ways than implementing lots of redundant stuff just to make one small feature to work?

NodeJs + Express + Angular6 + MongoDB

解决方案

Check Change Streams the newest and coolest feature of MongoDB 3.6 for accessing real time changes from database.

Sample Node.js Code:

const collection = db.collection('example');
const changeStream = collection.watch();
const next = await changeStream.next();

这篇关于当节点后端(MongoDB)中的状态更改时通知Angular前端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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