The below function (with debugging comments included) works exactly as expected in a testing script, but when used in the actual app it doesn’t work.
Function in question:
function getEnvelopeById(id) { //returns object if found, undefined if not
console.log("nEntered function getEnvelopeByIdn");
console.log("paramenter: id: " + id);
console.log("nenvelopesArray: ");
console.log(envelopesArray);
const e = envelopesArray.find((element) => element.Id === id);
console.log("nRan array method evelopesArray.findnResult returned: ");
console.log(e);
return e;
}
Below is the testing script where it works.
let envelopesArray = [];
class Envelope {
static _idCounter = 0;
constructor(title="default", budget=100) {
this._id = ++Envelope._idCounter;
this._title = String(title);
if(typeof budget === "number") { this._budget = budget; }
else { this._budget = 100; }
}
get Id() { return this._id; }
get Title() { return this._title; }
set Title(t) { this._title = t; }
get Budget() { return this._budget; }
set Budget(a) { this._budget = a; }
add(amount) { this._budget += amount; }
subtract(amount) { this._budget -= amount; }
static getId() { return Envelope._idCounter; }
} //class Envelope
function getEnvelopeById(id) { //returns object if found, undefined if not
console.log("nEntered function getEnvelopeByIdn");
console.log("paramenter: id: " + id);
console.log("nenvelopesArray: ");
console.log(envelopesArray);
const e = envelopesArray.find((element) => element.Id === id);
console.log("nRan array method evelopesArray.findnResult returned: ");
console.log(e);
return e;
}
function createEnvelope(obj) {
let newEnvelope = new Envelope(obj.title, obj.budget);
if(newEnvelope) { envelopesArray.push(newEnvelope); }
return newEnvelope;
}
createEnvelope({
title: "first",
budget: 100
});
createEnvelope({
title: "second",
budget: 200
});
const e = getEnvelopeById(1);
console.log(e);
Working output to console:
Entered function getEnvelopeById
paramenter: id: 1
envelopesArray:
[
Envelope { _id: 1, _title: 'first', _budget: 100 },
Envelope { _id: 2, _title: 'second', _budget: 200 }
]
Ran array method evelopesArray.find
Result returned:
Envelope { _id: 1, _title: 'first', _budget: 100 }
Envelope { _id: 1, _title: 'first', _budget: 100 }
App script where function no longer works (only necessary portions included):
let envelopesArray = [];
class Envelope {
static _idCounter = 0;
constructor(title="default", budget=100) {
this._id = ++Envelope._idCounter;
this._title = String(title);
if(typeof budget === "number") { this._budget = budget; }
else { this._budget = 100.00; }
}
get Id() { return this._id; }
get Title() { return this._title; }
set Title(t) { this._title = t; }
get Budget() { return this._budget; }
set Budget(a) { this._budget = a; }
add(amount) { this._budget += amount; }
subtract(amount) { this._budget -= amount; }
static getId() { return Envelope._idCounter; }
} //class Envelope
function getEnvelopeById(id) { //returns object if found, undefined if not
console.log("nEntered function getEnvelopeByIdn");
console.log("paramenter: id: " + id);
console.log("nenvelopesArray: ");
console.log(envelopesArray);
const e = envelopesArray.find((element) => element.Id === id);
console.log("nRan array method evelopesArray.findnResult returned: ");
console.log(e);
return e;
}
//testing purposes only//
createEnvelope({
title: "first",
budget: 100
});
createEnvelope({
title: "second",
budget: 200
});
console.log("Created test envelopes.nnenvelopesArray:");
console.log(envelopesArray);
console.log('n');
//end of testing area//
envelopesRouter.param("id", (req, res, next, id) => {
const found = getEnvelopeById(id);
if(found) {
req.envelope = found;
next();
} else {
res.status(404).send({message: `Could not find envelope with the ID ${id}`});
}
});
When the param endpoint calls the function, the console logging shows that it is returning undefined instead of the object as it does in the working test script.
Because the function works as expected in the test script, returning the object with the matching ID (_id), I am unsure what to change to make it work in the actual script where it only returns undefined.