gpt4 book ai didi

javascript - 如何从 model.find 中的查询参数设置对象的键

转载 作者:行者123 更新时间:2023-11-29 23:09:19 27 4
gpt4 key购买 nike

我想通过 model.find 方法中的查询参数生成对象的键。

我已经试过了:

moongoose.model.find({ req.query.type : req.params.tid})

还有这个:

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);

关于javascript - 如何从 model.find 中的查询参数设置对象的键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54108406/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com