为了在 URL 中传递单个参数,我在 Postman 中使用以下命令:
http://localhost:3000/api/prices/:shopId
这样可行!
现在,我想做的是将 shopId 替换为ShopIds 列表。
我对如何实现这一点有什么想法吗?
伪代码:
URL for shopId = 1: http://localhost:3000/api/prices/1
URL for shopId = 2: http://localhost:3000/api/prices/2
我应该怎么做才能在单个 API 响应中同时获取 shopId 1 和 2?
我有以下猫鼬模式:
const productSchema = new mongoose.Schema({
name: {
type: String,
required: true,
minlength: 1,
maxlength: 255
},
extraData: {
brand: {
type: String,
required: true,
minlength: 1,
maxlength: 255
},
quantity: {
type: Number,
required: true,
minlength: 1,
maxlength: 10
},
required: true
}
});
Run Code Online (Sandbox Code Playgroud)
但是,当我执行它时,我收到以下错误:“TypeError: Invalid schema configuration: Trueis not a valid type at path extraData.required”。我如何需要额外数据?
对于更新,我使用以下有效的代码:
router.put('/:id', async (req, res) => {
const { error } = validateProduct(req.body);
if (error) return res.status(400).send(error.details[0].message);
const product = await Product.findByIdAndUpdate(req.params.id,
{
name: req.body.name,
description: req.body.description,
category: req.body.category,
tags: req.body.tags,
withdrawn: req.body.withdrawn,
extraData: {
brand: req.body.extraData.brand,
quantity: req.body.extraData.quantity,
type: req.body.extraData.type
}
},
{new: true}
);
if (!product) return res.status(404).send('The product with the given ID was not found.');
res.send(product);
});
Run Code Online (Sandbox Code Playgroud)
我想要做的是创建一个 Patch 操作,它只更新某些字段,而不是像上面的更新那样更新所有字段。这些字段不是标准字段,但它们是更新操作的上述字段之一。
我有以下案例:
if condition {
if nestedCondition {
// logic
// I want to somehow break at this point but also be able
// to check the outer otherCondition
}
if otherNestedCondition {
// logic
}
}
if otherCondition {
//logic
}
Run Code Online (Sandbox Code Playgroud)
有没有办法“打破”nestedCondition但又能够检查otherCondition?