admin管理员组文章数量:1026446
Using bash, I think this is possible, but not sure about JavaScript, say we have this:
const {masterid} = req.query;
if (!masterid) {
return res.status(500).send(new Error('Missing query param "masterid".'));
}
What I want to do is not hardcode "masterid" in the string, instead do something like this:
const {masterid} = req.query;
if (!masterid) {
return res.status(500).send(new Error(`Missing query param "${Reflect(masterid).name()}.".`));
}
is there a way to do this with the Reflect API?
Using bash, I think this is possible, but not sure about JavaScript, say we have this:
const {masterid} = req.query;
if (!masterid) {
return res.status(500).send(new Error('Missing query param "masterid".'));
}
What I want to do is not hardcode "masterid" in the string, instead do something like this:
const {masterid} = req.query;
if (!masterid) {
return res.status(500).send(new Error(`Missing query param "${Reflect(masterid).name()}.".`));
}
is there a way to do this with the Reflect API?
https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Reflect
Share Improve this question edited Jul 12, 2021 at 12:56 Machavity♦ 31.7k27 gold badges95 silver badges105 bronze badges asked Sep 17, 2018 at 22:51 Alexander MillsAlexander Mills 101k166 gold badges539 silver badges919 bronze badges 2- I think the OP is similar to this question: stackoverflow./questions/7861956/… – Alexander Mills Commented Sep 17, 2018 at 22:54
- Not sure exactly what your use case is, but could you use a key/value setup instead? param = { key:"masterid", val="whatever" }. Then if you had a bunch of these you could loop through them and if the value is missing return res.status(500).send(new Error("missing " + iteratedParam.key)) – William Prevett Commented Sep 17, 2018 at 23:03
2 Answers
Reset to default 5From this other post
var masterid = 2;
var text = Object.keys({masterid})[0]; //equals "masterid"
This may be possible for testing/debugging purposes but is inappropriate in production. The only way is to tamper it in some way, e.g. parse it, modify and evaluate. For instance, rewire
uses eval
to intercept top-level module variables, this approach won't work with scoped variables.
Due to these JavaScript limitations, a proper approach is to not rely on variables. There will be no problems with handling property names. Assertions are mon in Node, this case can make use of helper function:
assertParam(obj, param) {
assert.ok(obj[param], `Missing param "${param}".`);
}
...
try {
assertParam(req.query, 'masterid');
} catch (err) {
return res.status(500).send(err);
}
Using bash, I think this is possible, but not sure about JavaScript, say we have this:
const {masterid} = req.query;
if (!masterid) {
return res.status(500).send(new Error('Missing query param "masterid".'));
}
What I want to do is not hardcode "masterid" in the string, instead do something like this:
const {masterid} = req.query;
if (!masterid) {
return res.status(500).send(new Error(`Missing query param "${Reflect(masterid).name()}.".`));
}
is there a way to do this with the Reflect API?
Using bash, I think this is possible, but not sure about JavaScript, say we have this:
const {masterid} = req.query;
if (!masterid) {
return res.status(500).send(new Error('Missing query param "masterid".'));
}
What I want to do is not hardcode "masterid" in the string, instead do something like this:
const {masterid} = req.query;
if (!masterid) {
return res.status(500).send(new Error(`Missing query param "${Reflect(masterid).name()}.".`));
}
is there a way to do this with the Reflect API?
https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Reflect
Share Improve this question edited Jul 12, 2021 at 12:56 Machavity♦ 31.7k27 gold badges95 silver badges105 bronze badges asked Sep 17, 2018 at 22:51 Alexander MillsAlexander Mills 101k166 gold badges539 silver badges919 bronze badges 2- I think the OP is similar to this question: stackoverflow./questions/7861956/… – Alexander Mills Commented Sep 17, 2018 at 22:54
- Not sure exactly what your use case is, but could you use a key/value setup instead? param = { key:"masterid", val="whatever" }. Then if you had a bunch of these you could loop through them and if the value is missing return res.status(500).send(new Error("missing " + iteratedParam.key)) – William Prevett Commented Sep 17, 2018 at 23:03
2 Answers
Reset to default 5From this other post
var masterid = 2;
var text = Object.keys({masterid})[0]; //equals "masterid"
This may be possible for testing/debugging purposes but is inappropriate in production. The only way is to tamper it in some way, e.g. parse it, modify and evaluate. For instance, rewire
uses eval
to intercept top-level module variables, this approach won't work with scoped variables.
Due to these JavaScript limitations, a proper approach is to not rely on variables. There will be no problems with handling property names. Assertions are mon in Node, this case can make use of helper function:
assertParam(obj, param) {
assert.ok(obj[param], `Missing param "${param}".`);
}
...
try {
assertParam(req.query, 'masterid');
} catch (err) {
return res.status(500).send(err);
}
本文标签: javascriptGet variable name instead of valueStack Overflow
版权声明:本文标题:javascript - Get variable name instead of value - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745645195a2160983.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论