这与其说是一个问题,不如说是一个咨询请求。我找不到资源来检查我的方法的有效性,所以我想听听 MongoDB 专家的意见。
我正在研究 MongoDB,并想出了这个中间件方法来将客户端传递到我的路由。我有这个 Express 中间件:
const addClientToRequest = async (req, _, next) => {
const client = new MongoClient(uri);
await client.connect();
req.client = client;
next();
};
app.use(addClientToRequest);
Run Code Online (Sandbox Code Playgroud)
之后,我在路由中使用 req.client 来访问我的数据库。
app.get("/:id", async (req, res) => {
const client = req.client;
const id = req.params.id;
try {
const data = await client.db("mydb").collection("mycollection").findOne({ id });
if (data) return res.status(200).json(data);
} catch (error) {
return res
.status(500)
.json({ message: "Error fetching requested data", error });
}
return res.status(404).json({ message: "Requested data …Run Code Online (Sandbox Code Playgroud) 我的组件接受一个overlay应该是有效 CSS 颜色的道具。
当我在样式对象中CTRL + Click的color属性上时,类型定义来自csstypenode_modules 中的文件夹。CSS 颜色属性的类型定义定义为Property.Color. 我将该类型导入到我的组件中并将其用作我的overlay道具类型,但是any当我尝试使用该组件时它最终会出现。
我的组件的类型定义:
import { Property } from "../node_modules/csstype/index";
export interface BlurredComponentProps {
overlay?: Property.Color;
}
Run Code Online (Sandbox Code Playgroud)
这是我使用组件时的样子:
所以我的问题是,如何正确输入一个应该只采用有效 CSS 颜色的道具,如果给出了非颜色值,则给出错误?
谢谢