Node function not working, await part is not getting executed in the given function

Viewed 30

While hitting API I'm getting function from my services/ElasticSearch.js and for some reason function there is not working after the axios part.

In the below file I've called function elasticService.updateDocument this function has been brought from another file.

'''

class ProductController {
  constructor() { }
  async saveProduct(req, res) {
    console.log('ITs coming here')
    let { _id, Product } = req.body;
    if (_id) delete req.body._id;
    let elasticResult;
    try {
      if (Product && Product.Category) {
        req.body.Category = Product.Category
        delete Product.Category
      }
      if (Product && Product.URL) {
        const exists = await ProductService.checkProductByUrl(Product.URL);
        _id = exists._id
      }
      const result = await ProductService.saveProduct(req.body, _id);
      if (result) {
        if (_id) {
          console.log('Here.... UPDATE')
          const savedProduct = await ProductModel.createPayload(req.body);
          console.log(savedProduct,'saved_product')
          let elaticDoc = await this.createElasticDocData(savedProduct);
          console.log(elaticDoc.id,'elasticResult')
          elaticDoc.id = result._id;
          elaticDoc = new Elastic(elaticDoc);
          console.log(elaticDoc,'<----------elaticdoc-------------->')
          elasticResult = await elasticService.updateDocument(JSON.stringify(elaticDoc), req.body.Category)
          console.log(elasticResult,'elasticResult')
        }
        else {
          console.log('Here.... ADD')
          const savedProduct = await ProductModel.createPayload(result);
          let elaticDoc = await this.createElasticDocData(savedProduct);
          elaticDoc.id = result._id;
          elaticDoc = new Elastic(elaticDoc);
          elasticResult = await elasticService.createDocument(JSON.stringify(elaticDoc), req.body.Category)
        }
        const response = new Response(1, "Product is saved successfully", "", "", { product: result, elasticResult: elasticResult });
        return res.status(200).send(response);
      }
      const response = new Response(0, "Error in saving Product", 0, "Product not saved", {});
      return res.status(200).send(response);
    } catch (error) {
      const response = new Response(0, "Unexpected Error", 0, error, {});
      return res.status(400).send(response);
    }
  }

'''

This is the elasticappsearch file where above mentioned is coming from and for some reason it's not working after axios.patch part.

''' const private_key = process.env.elastic_private_key const search_key = process.env.elastic_search_key

const axios = require("axios")

class ElasticAppSearch {

 async updateDocument(body, engine) {
        console.log('Its coming in updateDOCS here')
        const response = await axios.patch(`${process.env.elastic_url}/${engine}/documents`, body, {
            headers: {
                Authorization: `Bearer ${private_key}`,
            },
        });
        console.log(response,'<--===-=-=-=-=-=-=-=-=-=-=-=-response')
        return response.data
    }

'''

0 Answers
Related