小编Gia*_*nis的帖子

如何在 Express.js 中传递 URL 中的字符串列表

为了在 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?

node.js express postman

5
推荐指数
1
解决办法
7241
查看次数

如何在 Mongoose Schema 中要求嵌套的 json 对象

我有以下猫鼬模式:

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”。我如何需要额外数据?

mongoose node.js express

5
推荐指数
1
解决办法
1215
查看次数

Node.js 和 Mongoose 中的补丁请求方法

对于更新,我使用以下有效的代码:

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 操作,它只更新某些字段,而不是像上面的更新那样更新所有字段。这些字段不是标准字段,但它们是更新操作的上述字段之一。

mongoose mongodb node.js express

3
推荐指数
1
解决办法
7297
查看次数

Go 中中断 if 语句的简洁方法

我有以下案例:

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

go

1
推荐指数
2
解决办法
2万
查看次数

标签 统计

express ×3

node.js ×3

mongoose ×2

go ×1

mongodb ×1

postman ×1