javascript - How can I avoid logging into SalesForce databases on every router in Express.js? -
i need log salesforce databases , pass query. passing lot of queries on many routers of express.js , real pain login in every router. please let me know if know how can avoid this.
var conn = new jsforce.connection({ oauth2 : salesforce_credential.oauth2 }); var username = salesforce_credential.username; var password = salesforce_credential.password; // want avoid login on every router conn.login(username, password, function(err, userinfo) { if (err) { return console.error(err); } conn.query("select id sourcing__c id = 'req.session.ref'",function(err, result) { if (err) { return console.error(err); } if(result.records.length === 0){ req.session.ref = ""; } var body = { "auth__c": req.user.id, "stus__c": "pending - new hire", "record": "012lviac", "sourcing__c": req.session.ref }; conn.sobject("sfdc_employee__c").create(body, function(err, ret) { if (err || !ret.success) { return console.error(err, ret); } console.log("created record id : " + ret.id); // ... }); }); });
you may save login status , check every time when query, here add property on conn (the instance of 'jsforce.connection'), i'm not sure think there may method or property show login status of 'conn', may dig documents.
var conn = new jsforce.connection({ oauth2 : salesforce_credential.oauth2 }); var username = salesforce_credential.username; var password = salesforce_credential.password; conn._islogin = false; // here in route handler if (!conn._islogin) { conn.login(username, password, function(err, userinfo) { if (err) { return console.error(err); } conn._islogin = true; doquery(conn); }); } else { doquery(conn); } function doquery (conn) { conn.query("select id sourcing__c id = 'req.session.ref'",function(err, result) { if (err) { return console.error(err); } if(result.records.length === 0){ req.session.ref = ""; } var body = { "auth__c": req.user.id, "stus__c": "pending - new hire", "record": "012lviac", "sourcing__c": req.session.ref }; conn.sobject("sfdc_employee__c").create(body, function(err, ret) { if (err || !ret.success) { return console.error(err, ret); } console.log("created record id : " + ret.id); // ... }); }); }
Comments
Post a Comment