Edit: Don’t bother answering if anyone ever does. I’m dumping DF and changing to the Nest.js / Node.js stack. More community support. Too bad about DF. DF is a great product and should be more popular, especially with those of us new to server side coding and there are a lot of us in the JavaScript world.
Original post:
This function isn’t retrieving the data from the database. Details below.
function getSkillRecord(id) {
var url = 'pfcrud/_table/skills/' + id;
return new Promise(function (resolve, reject) {
platform.api.get(url, null, function (body, response) {
var result = JSON.parse(body);
console.log('result: ', result);
if (result !== "" || result !== null) {
resolve(result.skill_name);
} else {
reject(Error("Fetch from database failed."));
}
})});
}
This code lives in the Scripts tab with this process:
myService._table.purchaseOrders.get.post_process
Local install in Vagrant Ubuntu 16.04 box with Postgres 9.5 and DF 2.9.0.
A function above the first one loops through an array of item ids in a Postgres field, creates an array like: idArray = [11, 12, 14] and record = allRecords[i]. It then assigns values to the two parameters in this function. That works fine as does the function below, getItemNames(). I tested getItemNames with a fake db array of objects on the page. The Postgres table has columns for item_id and item_name, as does my fake db table.
The problem is in the getItemRecord(id) that uses the DF Node.js API in the Scripts tab. It is supposed to fetch the item object by its id from the items table and then I assign the item_name to a var to pass to the calling function. The commented function below it works fine for tests so I’ve isolated the problem to the DF API setup. I followed the official docs as best I could.
I’m logged in as Admin and have Roles for the tables set to Api, Script.
I include promises for async, but I’m new to all of this and not sure I’m using them correctly.
Any ideas?
// Everything in this function works fine in tests. The problem is below.
// Called from for loop above.
// Process the idArray to extract item_name's from items table.
// Includes getItemRecord() to retrieve each item name from the db.
function getItemNames(record, idArray) {
var item_names = [];
var itemName;
var id;
return new Promise(function (resolve, reject) {
for (var i = 0; i < idArray.length; i++) {
id = idArray[i];
if (id !== null) {
// Get the record for this id.
itemName = getItemRecord(id); // This works if accessing a local var fake db.
// Creates an array of item names for member. Works perfect in tests.
resolve(item_names.push(itemName));
} else {
reject(Error("Item names parse failed."));
}
}
// Last step to complete the replacement of item id's with item names for a record.
// This changes the json sent to the UI.
record.item_id_array = item_names;
});
}
// This fails to retrieve records. The commented function below works.
function getItemRecord(id) {
var url = 'myService/_table/items/' + id;
return new Promise(function (resolve, reject) {
platform.api.get(url, null, function (body, response) {
var result = JSON.parse(body);
if (result !== "" || result !== null) {
resolve(result.item_name);
} else {
reject(Error("Fetch from database failed."));
}
})});
}
/* This works for retrieving records from a local var fake db.
function getItemRecord(id) {
var dbRecord = fakeDB.find(function (dbRecord) { return dbRecord.item_id === id; });
return dbRecord.item_name;
}
*/