【发布时间】:2021-02-25 15:28:15
【问题描述】:
我有一个 json 对象,我将其作为参数传递给 axios get api。这是 Json 对象
obj = {
name: "device"
value: true,
}
我正在使用上述对象调用 axios get 请求,如下所示 -
tableFilter = (obj) => {
console.log(typeof obj.value);
axios
.get("http://localhost:3200/getFilter/", { params: obj })
.then((res) => {
console.log(res.data);
});
};
在上面的代码中,'obj.value' 的类型在控制台中显示为 'boolean'。现在我像这样在服务器端处理这个请求-
Router.get("/getFilter", async (req, res) => {
console.log(typeof req.query.value);
dbase.collection("employee").find(
{ [req.query.filter]: req.query.value },
{ projection: { _id: 0 } })
.toArray().then((result) => {
console.log(result);
});
}
});
'Router' 是快速路由器对象,'dbase' 是我的数据库的 mongoclient 对象(它们与我的问题无关)。在上面的代码中,如果我控制对象“值”键,它会将类型显示为“字符串”。为什么布尔值会自动转换为字符串? 我需要根据字段“设备:真”查询mongodb文档所以我需要布尔值而不是字符串。
【问题讨论】:
标签: javascript node.js reactjs axios mongodb-query