javascript - How to set the key of the object from query params in model.find
问题描述
I want to make the key of the object from query params in the model.find
method.
I have already tried this:
moongoose.model.find({ req.query.type : req.params.tid})
and this:
let type = req.query.type
moongoose.model.find({type : req.params.tid})
解决方案
如果要将变量值用作键,则需要使用方括号:
const type = req.query.type;
mongoose.model.find({[type]: req.params.tid});
一个例子:
const key = 'myKey';
const obj = { [key]: 'value' };
console.log(obj);
推荐阅读
- python - 寻找 3D 线性回归线的斜率
- wordpress - 将whatsapp分享按钮添加到php插件
- matplotlib - plt.fill_between()方法的解释?讨论
- ruby-on-rails - Ruby on Rails 和 Sendinblue 通过 SMTP
- java - Maven Shade 插件不会重新定位公共日志记录
- python - 如何让 Python Discord 机器人等待某个测验的答案?
- flutter - 使用 Flutter 和 PHP Laravel 注册用户并使用 SharedPreferrence 保存用户信息
- macos - Automator - 将文件移动到新创建的文件夹
- tensorflow - 如何在 Keras 自定义损失中评估 B-Splines
- python - 如何在 Python 中将数据写入 csv 文件