W
W
Wasya UK2020-10-04 17:04:58
MongoDB
Wasya UK, 2020-10-04 17:04:58

How to update information in the database?

Is it correct to do such updates of information in the project? Or is it better to somehow do everything through 1 request, without error control?

async updateProduct(id, productData) {
    const product = await Product.findById(id);
    if (!product) {
      throw new Error(PRODUCT_NOT_FOUND);
    }
    if (await this.checkProductExist(productData, id)) {
      throw new Error(PRODUCT_ALREADY_EXIST);
    }
    const model = await modelService.getModelById(productData.model)
    productData.model = model.name;
    return Product.findByIdAndUpdate(id, productData, { new: true });
  }

P.S. there are 3 calls to the database...

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question