Alright, so this question is an extension of this question and related to this question. My code calling the function format can be seen in the first question. This is the code in my transform:
format() {
return new Promise((resolve, reject) => {
return Database.getInstance().getModel('feature_default').findAll({})
.then((databaseObjects) => {
this.getJsonObject().features = {};
for (let databaseObject of databaseObjects) {
console.log('ADD FEATURE DEFAULT TO ARRAY ' + databaseObject.id);
this.getJsonObject().features[databaseObject.id] = JSON.parse(databaseObject.default_json);
}
console.log('RESOLVE FEATURE DEFAULT');
return resolve(this.getJsonObject());
})
.catch((e) => {
return reject(e);
});
});
}
I tried to remove the anti-pattern and I'm seeing the exact same behavior:
format() {
return Database.getInstance().getModel('feature_default').findAll({})
.then((databaseObjects) => {
this.getJsonObject().features = {};
for (let databaseObject of databaseObjects) {
console.log('ADD FEATURE DEFAULT TO ARRAY ' + databaseObject.id);
this.getJsonObject().features[databaseObject.id] = JSON.parse(databaseObject.default_json);
}
console.log('RESOLVE FEATURE DEFAULT');
return this.getJsonObject();
})
.catch((e) => {
throw e;
});
}
Here is the code calling format:
var promises = databaseObjects.map(databaseObject => {
var jsonObject = {};
var transform = new Transform();
transform.setDatabaseObject(databaseObject);
transform.setJsonObject(jsonObject);
transform.baseFormat()
.then(() => transform.format())
.then(() => {
res.locals.retval.addData(jsonObject);
res.locals.retval.addErrors(transform.getErrors());
res.locals.retval.addWarnings(transform.getWarnings());
}).catch((e) => {
console.log('Caught error during format of existing object: ');
console.log(e);
this.error(e, res);
throw e;
});
});
Promise.all(promises)
// ...
I've tried to do a Promise.all
like in my second question, where each promise adds a feature to the array, but that did not fix this issue either.
Using this call, when I first query the server, I get the correct result:
{
"errors": [],
"warnings": [],
"data": [
{
"id": 1,
"name": "All",
"enabled": 1,
"features": {
...
}
}
]
}
However, on any subsequent calls I receive this:
{
"errors": [],
"warnings": [],
"data": []
}
I am not changing any parameters or the call at all. I believe I'm failing to lock a Promise but I don't see where as I'm returning everywhere I should.