node.js - 定义队列将在 Nodejs 中接受的消息类型
问题描述
使用的技术堆栈是Typescript、Express、Nodejs、Mongoose 和 Mongodb。
Nodejs 的Bull库用于管理Queues。
我们可以限制队列接受的消息类型吗?
const data1 = {name: "Alpha", Age: 30};
const data1 = {name: "Beta", Age: 40};
const data3 = {color: "Red", taste: "sweet"}; // queue should give error on some other message structire being passed
queue.add(data1);
queue.add(data2);
queue.add(data3); // error should come here
queue.add
在泛型的帮助下,使用 Java 等语言很容易做到这一点。
在打字稿中实现相同的任何方法?
解决方案
您应该能够在实例化队列期间提及您想要的特定类型 -
interface QueueData {
name: string;
Age: number;
}
const myQueue = new Queue<QueueData>('name', options);
推荐阅读
- php - PHP:将属性设为特征私有
- java - 应用程序下载网页不起作用
- android - 元素在 GridLayout 中不可见
- css - 滚动表格时的两个固定列
- openedge - OpenEdge Progress 4GL WRITE-XML NAMESPACE-PREFIX
- java - 如何使用 JsonPath 向 Json 添加新节点?
- unity3d - 如何将 EditorGUILayout.EnumPopup 集成为 EditorGUILayout.IntSlider 的标签?
- python - KeyError:无 + argparse python 3.xx
- spring - Springboot中如何处理“org.springframework.beans.factory.BeanCreationException”异常
- java - 网络中的观察者模式