我正在尝试实现一个驱动程序,该程序将提供我的流程JSON对象。基本上,我需要从.csv文件中获取消息并通过STOMP发布。然后,我将有一个订阅者,它将监听新消息,进行计算并推送到mongo数据库。
const Sleep = require('sleep');
const CSV = require('csvtojson');
const MongoClient = require('mongodb').MongoClient;
const ParseJson = require('parse-json');
const Stomp = require('stomp-client');
const StompClient = new Stomp([hostname], 61613, [username], [password]);
var db;
MongoClient.connect(url, { useNewUrlParser: true }, (err, database) => {
if (err) { throw err; }
db = database.db([db_name]);
});
StompClient.connect( (connected, rejected) => {
if ( rejected ) { throw rejected; }
StompClient.subscribe([destination], (body, headers) => {
console.log("Received message", body);
});
CSV()
.fromFile([file_path])
.then( (jsonArray) => {
jsonArray.forEach( (json) => {
StompClient.publish([destination], json);
Sleep.sleep(1);
});
});
});
我收到消息头,但是正文始终为空。如果我硬编码一条消息,那么我会收到尸体。这与我在Promise中处理结果有关吗?我该如何解决这个问题?