Wednesday 18 October 2017

Struggling to get a committed transaction using NodeJS and MSSQL

function UpdateDatabase(cd){ var Character_ID = -1 try { sql.connect(config).then(pool => { console.log("This happening?"); pool.request() .input('Url', sql.nvarchar, cd.url) .input('Character', sql.nvarchar, cd.Character) .input('Player', sql.nvarchar, cd.Player) .input('Species', sql.nvarchar, cd.Species) .input('Specializations', sql.nvarchar, cd.Specializations) .input('System', sql.nvarchar, cd.System) .input('Brawn', sql.int, cd.Brawn) .input('Agility', sql.int, cd.Agility) .input('Intellect', sql.int, cd.Intellect) .input('Cunning', sql.int, cd.Cunning) .input('Willpower', sql.int, cd.Willpower) .input('Presence', sql.int, cd.Presence) .input('Soak', sql.int, cd.Soak) .input('WoundsThreshold', sql.int, cd.WoundsThreshold) .input('StrainThreshold', sql.int, cd.StrainThreshold) .input('RangedDefense', sql.int, cd.RangedDefense) .input('MeleeDefense', sql.int, cd.MeleeDefense) .output('Character_ID', sql.int) .execute('AddUpdateCharacter',(err,result) => { console.log(err); console.log(result); }) }).then(result => { Character_ID = result[0]; if(Character_ID != -1){ $(cd.Skills).each(function(key,value){ pool.request() .input('Character_ID', sql.int, Character_ID) .input('Skill', sql.nvarchar, value.Skill) .input('Career', sql.bit, value.Career) .input('SkillLevel', sql.int, value.Level) .output('CharacterSkill_ID', sql.int) .execute('dbo.AddUpdateCharacter') }) $(cd.Talents).each(function(key,value){ pool.request() .input('Character_ID', sql.int, Character_ID) .input('Talent', sql.nvarchar, value.Talent) .output('CharacterTalent_ID', sql.int) .execute('dbo.AddUpdateCharacter') }) } }) } catch(err){ console.log(err); } sql.close(); }; I have no errors. I have no messages. "This happening?" does not print. However when I remove the sql.close() it throws a "Global connection already exists. Call sql.close() first", the "This happening?" message as well as the following warrnings:(node:12584) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: Cannot read property 'type' of undefined (node:12584) DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.I feel like this is wrong as it's opening a global connection to the database and it should be opening a single connection to the database. I see a connection opened on a SQL trace but I see no execution.

Submitted October 18, 2017 at 09:58PM by Thriven

No comments:

Post a Comment