下面的函数(包含调试注释)在测试脚本中完全按照预期工作,但在实际应用程序中使用时它不起作用。
有问题的功能:
function getEnvelopeById(id) { //returns object if found, undefined if not
console.log("\nEntered function getEnvelopeById\n");
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.find\nResult returned: ");
console.log(e);
return e;
}
下面是它的工作测试脚本。
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 getEnvelopeById\n");
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.find\nResult 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);
控制台的工作输出:
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 }
功能不再起作用的应用程序脚本(仅包括必要的部分):
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 getEnvelopeById\n");
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.find\nResult returned: ");
console.log(e);
return e;
}
//testing purposes only//
createEnvelope({
title: "first",
budget: 100
});
createEnvelope({
title: "second",
budget: 200
});
console.log("Created test envelopes.\n\nenvelopesArray:");
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}`});
}
});
当参数端点调用该函数时,控制台日志记录显示它返回未定义而不是像在工作测试脚本中那样返回对象。
因为该函数在测试脚本中按预期工作,返回具有匹配 ID (_id) 的对象,所以我不确定要更改什么才能使其在实际脚本中工作,而实际脚本仅返回未定义。
解决方案是将
===
比较运算符更改为==
。正如@Pointy 观察到的,测试传递的是一个数字,而在实际代码中它是一个字符串。