Monday 26 August 2019

javascript - How can i return status inside the promise?

I started to learning promise with loopback and jsforce, and couldn't handle this problem; I couldn't return status var inside promise to cb() function.
Basically i want to connect salesforce and get data via JSforce and write it to db via loopback. Then want to return created/updated/error records to client after remote mothed called.



I'm developing with Loopback via using Node.JS & Express.js
I'm using JSforce library to connect salesforce



How can I fix that?



Here is my code:




module.exports = function(Contact) {
var jsforce = require('jsforce');
var async = require("async");
var lr = require('lr.js');

Contact.ImportContacts = function(cb) {
// Salesforce Projects List
var sf_projects = [];
//Salesforce Conn String

var conn = lr.SalesforceConn();
conn.apex.get("/Contact/", function(err, res) {
var status = {
"Created": [],
"Updated": [],
"Error": ""
};
if (err) console.log(err);

sf_projects = res;

// Clear result
status.Created.length = 0;
status.Updated.length = 0;
status.Error = "";

if (sf_projects != undefined) {
async.eachSeries(sf_projects, function(contact, callback) {
Contact.findOrCreate({
where: {
co_SalesforceID: contact.Id

}
}, {
co_Name: contact.FirstName,
co_Surname: contact.LastName,
co_Salutation: contact.Salutation,
co_Title: contact.Title,
co_Department: contact.Department,
co_Email: contact.Email,
co_PhonePersonal: contact.HomePhone,
co_PhoneWork: contact.Phone,

co_PhoneCell: contact.MobilePhone,
co_Description: contact.Description,
co_SalesforceID: contact.Id
},
function(err, cntct, created) {
if (err) console.log(err);
if (created) {
status.Created.push(cntct.id);
console.log("Contact created. SalesForeID: " +
cntct.co_SalesforceID +

" ContactName: " +
lr.isDefined(cntct.co_Salutation) + " " +
lr.isDefined(cntct.co_Name) + " " +
lr.isDefined(cntct.co_Surname));
} else {
Contact.replaceById(cntct.id, {
co_Name: contact.FirstName,
co_Surname: contact.LastName,
co_Salutation: contact.Salutation,
co_Title: contact.Title,

co_Department: contact.Department,
co_Email: contact.Email,
co_PhonePersonal: contact.HomePhone,
co_PhoneWork: contact.Phone,
co_PhoneCell: contact.MobilePhone,
co_Description: contact.Description,
co_SalesforceID: contact.Id
},
false,
function(err, obj) {

if (err) console.log(err);
status.Updated.push(obj.id);
console.log("Contact updated. SalesForeID: " +
obj.co_SalesforceID + " ContactName: " +
lr.isDefined(obj.co_Salutation) + " " +
lr.isDefined(obj.co_Name) + " " +
lr.isDefined(obj.co_Surname));
});
}
});

callback(err);
}, function(err) {
if (err) console.error(err);
});
} else {
console.log("Salesforce Connection Error!");
status.Error = "Salesforce Connection Error";
}
return Promise.resolve(status);
}).then(function(end) {

cb(null, end);

}).catch(function(err) {
if (err) console.log(err);
});
};
Contact.remoteMethod(
'ImportContacts', {
returns: {
arg: 'result',

type: 'string'
},
http: {
path: '/importContacts',
verb: 'get'
}
}
);
};

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...