我開始學習使用loopback和jsforce的諾言,並且無法處理這個問題;我無法將狀態變量返回給cb()函數。 基本上我想連接salesforce並通過JSforce獲取數據並通過回送將其寫入數據庫。然後想要在遠程調用後調用創建/更新/錯誤記錄到客戶端。如何返回承諾內的狀態?
我與迴環通過使用Node.js的& Express.js 開發我使用JSforce庫連接的Salesforce
我怎樣才能解決呢?
這裏是我的代碼:
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'
}
}
);
};
你想從那裏究竟這個牆上的文字內回覆? – deceze
我想從conn.apex.get()中返回狀態變量,並將其賦予給cb()函數 – canerce
也許你可以嘗試類似的方式解決:解決方案({status:status,callback:cb}) ; 並且在隨後塊: 。然後(函數(端){ end.callback(NULL,end.status); })進行更改應用凍結和應用後 – 1337