问题是一切都可以正常工作,但是只要我将Log.create(obj)组合到Neo4j结果流中,它就会停止工作,我认为是一些Promise行为正在吃掉所有日志信息所以我无法弄清楚为什么它不起作用或如何强迫它显示有意义的信息。
过去两天我一直试图对此进行诊断,但我无法弄清楚原因。
我有2个文件: example.js和E1Logger / index.js
logger文件只是将对象写入MongoDB的方法的模式和模型。如果我注释掉example.js的下半部分,它的工作正常。
我尝试过20种不同的链接承诺组合,捕获,完成,执行,使它更加异步,使其更加同步,而且我似乎无法获得任何有意义的信息。
我很想知道是什么让它发挥作用。
example.js
//var config = require('./config');
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/major', error => { if (error) { console.error('ERROR: ' + error) }});
//mongoose.Promise = global.Promise;
var neo4j = require('neo4j-driver').v1;
var config = {};
config.neo4jHost = 'bolt://localhost:7687';
config.neo4jUser = 'neo4j';
config.neo4jPassword = 'garbagepassword';
var driver = neo4j.driver(config.neo4jHost, neo4j.auth.basic(config.neo4jUser, config.neo4jPassword));
var E1Logger = require('./E1Logger');
let newInfoLog = {
logType: 'INFO',
eventType: 'Purge Stale Invite',
result: 'Success',
invitedByUser: {
neo4jNodeID: 123,
name: 123 + ' ' + 321,
email: 123
},
purgedUser: {
neo4jNodeID: 123,
email: 123
},
createdAt: 123,
expirationAt: 123
};
let newErrorLog = {
logType: 'ERROR',
eventType: 'Purge Stale Invite',
result: 'Fail',
reason: 123,
attemptedQuery: 123,
};
E1Logger.info(newInfoLog);
E1Logger.error(newErrorLog);
// Calculate cutoff time for expired invites and form the Cypher Query
var currentTime = new Date().getTime();
var sliceTime = currentTime - 604800000; // 604800000 milliseconds in one week
var cypherQuery = 'MATCH (e:Person)-[r:INVITED_TO_APP]->(x) WHERE r.expirationAt<=\'' + sliceTime + '\' AND r.status=\'pending\' RETURN id(e) as invitedByUserId, e.given_name as invitedByUserFirstName, e.family_name as invitedByUserLastName, e.email as invitedByUserEmail, r.createdAt as createdAt, r.expirationAt as expirationAt, id(x) as purgedUserId, x.email as purgedUserEmail';
// Execute Purge Event
var session = driver.session();
session
.run(cypherQuery)
.then(function(result){
result.records.forEach(function(record) {
// Generate log for each expired invite
let newInfoLog = {
logType: 'INFO',
eventType: 'Purge Stale Invite',
result: 'Success',
invitedByUser: {
neo4jNodeID: record.get('invitedByUserId').low,
name: record.get('invitedByUserFirstName') + ' ' + record.get('invitedByUserLastName'),
email: record.get('invitedByUserEmail')
},
purgedUser: {
neo4jNodeID: record.get('purgedUserId').low,
email: record.get('purgedUserEmail')
},
createdAt: record.get('createdAt'),
expirationAt: record.get('expirationAt')
};
// Log result
console.log('PURGED: ' + JSON.stringify(newInfoLog));
E1Logger.info(newInfoLog);
});
session.close();
driver.close();
})
.catch(function(error) {
// Generate log for failed purge
let newErrorLog = {
logType: 'ERROR',
eventType: 'Purge Stale Invite',
result: 'Fail',
reason: error,
attemptedQuery: cypherQuery,
};
// Log result
console.log('ERROR: ' + JSON.stringify(newErrorLog));
E1Logger.error(newErrorLog);
//session.close();
//driver.close();
});
mongoose.disconnect();
E1Logger / index.js
const mongoose = require('mongoose');
// Define Info Log Schema
const InfoLogModel = mongoose.model('Infolog', new mongoose.Schema({
logType: String,
loggedAt: { type: Number, default: new Date().getTime() },
eventType: String,
result: String,
invitedByUser: {
neo4jNodeID: Number,
name: String,
email: String
},
purgedUser: {
neo4jNodeID: Number,
email: String
},
createdAt: Number,
expirationAt: Number
}));
// Define Error Log Schema
const ErrorLogModel = mongoose.model('Errorlog', new mongoose.Schema({
logType: String,
loggedAt: { type: Number, default: new Date().getTime() },
eventType: String,
result: String,
reason: String,
attemptedQuery: String,
}));
module.exports = {
// Define Info Log Model
info: function(eventData) {
InfoLogModel.create(eventData, (error, addedEvent) => {
if (error) throw error;
console.log('INFO: ' + addedEvent);
});
},
// Define Error Log Model
error: function(eventData) {
ErrorLogModel.create(eventData, (error, addedEvent) => {
if (error) throw error;
console.log('ERROR: ' + addedEvent);
});
}
};
我怀疑问题发生在ErrorLogModel.create(eventData)
。我从来没有能够从session.run()内部写入MongoDB。 A有几个组合显示了一个关于承诺拒绝的东西,但这与我得到的一样接近。我记得ErrorLogModel.create(eventData).catch( err => console.log('ok') );
这样的事情让它做到了。
我在这里错过了一些承诺吗?为什么功能代码通过Neo4j运行代码session.run().then()
导致它停止工作?这可能是我应该向Mongoose或Neo4j报告的错误吗?