简体   繁体   中英

connection.queryRaw is not a function error in NodeJs WebAPI call

I am new to Nodejs. I am developing WebAPI by using NodeJs and MSSQl as database.My api is giving proper response in case of POST endpoint If it is called while server is listening through Dev environment command [npm run start]. But, If I Deploy my API on Windows IIS, it is giving the mentioned error. My reference API endpoint code is as below:

router.post('/',async (req,res,next)=>{
    // console.log('Enter products creation')
    const Product = Array.from(req.body) // req.body  

    // console.log('New Product details passed on',Product)
    const createProd = require('../CreateProduct')
    const response = await createProd(Product)

    res.status(404).json({
        message : response.retStatus
    })
})

CreateProduct function called in above code is as below:

const sql = require("mssql/msnodesqlv8");
const dataAccess = require("../DataAccess");

const fn_CreateProd = async function (product) {
  let errmsg = "";
  let objBlankTableStru = {};
  let connPool = null;
  // console.log('Going to connect with Connstr:',global.config)
  await sql
    .connect(global.config)
    .then((pool) => {
      global.connPool = pool;
      
      productsStru = pool.request().query("DECLARE @tblProds tvp_products select * from @tblProds");

      return productsStru;
    })
    .then(productsStru=>{
      objBlankTableStru.products = productsStru

      productsOhStru = global.connPool.request().query("DECLARE @tblprodsOh tvp_product_oh select * from @tblprodsOh");
      
      return productsOhStru
    })
    .then((productsOhStru) => {
      objBlankTableStru.products_oh = productsOhStru
      
      let objTvpArr = [
        {
          uploadTableStru: objBlankTableStru,
        },
        {
          tableName : "products",
          tvpName: "tvp_products",
          tvpPara: "tblProds"
        },
        {
          tableName : "products_oh",
          tvpName: "tvp_product_oh",
          tvpPara: "tblProdsOh",
        }        
      ];

      newResult = dataAccess.getPostResult(
        objTvpArr,
        "sp3s_ins_products_tvp",
        product
      );

      console.log("New Result of Execute Final procedure", newResult);
      return newResult;
    })
    .then((result) => {
      // console.log("Result of proc", result);
      if (!result.recordset[0].errmsg)
        errmsg = "New Products Inserted successfully";
      else errmsg = result.recordset[0].errmsg;
    })
    .catch((err) => {
      console.log("Enter catch of Posting prod", err.message);
      errmsg = err.message;
      if (errmsg == "") {
        errmsg = "Unknown error from Server... ";
      }
    })
    .finally((resp) => {
      sql.close();
    });
  return { retStatus: errmsg };
};

module.exports = fn_CreateProd;

GetPost() function is as below:

const getPostResult = (
  tvpNamesArr,
  procName,
  sourceData,
  sourceDataFormat,
  singleTableData
) => {
  let arrtvpNamesPara = [];
  let prdTable = null;
  let newSrcData = [];

  // console.log("Source Data :", sourceData);
  let uploadTable = tvpNamesArr[0];

  for (i = 1; i <= tvpNamesArr.length - 1; i++) {
    let tvpName = tvpNamesArr[i].tvpName;
    let tvpNamePara = tvpNamesArr[i].tvpPara;
    let TableName = tvpNamesArr[i].tableName;

    let srcTable = uploadTable.uploadTableStru[TableName];
    srcTable = srcTable.recordset.toTable(tvpName);

    let newsrcTable = Array.from(srcTable.columns);

    newsrcTable = newsrcTable.map((i) => {
      i.name = i.name.toUpperCase();
      return i;
    });
    if (!singleTableData) {
      switch (sourceDataFormat) {
        case 1:
          newSrcData = sourceData.filter((obj) => {
            return obj.tablename.toUpperCase() === TableName.toUpperCase();
          });
          break;
        case 2:
          newSrcData = getObjectDatabyKey(sourceData, TableName);
          break;
        default:
          newSrcData = getTableDatabyKey(sourceData, TableName);
          break;
      }
    } else {
      newSrcData = sourceData;
    }

    // console.log(`Filtered Source data for Table:${TableName}`, newSrcData);
    prdTable = generateTable(
      newsrcTable,
      newSrcData,
      tvpName,
      sourceDataFormat
    );

    arrtvpNamesPara.push({ name: tvpNamePara, value: prdTable });
  }

  const newResult = execute(procName, arrtvpNamesPara);
  return newResult;
};

Finally, I have found the solution to this.. it is very strange and shocking and surprising that If I using Morgan Middleware in app.js and have used it by syntax: app.use(morgan('dev')) , then it is the culprit command..I just removed dev from this command after which problem got resolved..But I found no help regarding this issue over Google anywhere..I really fear that what type of challenges I am going to face in future development if these kind of silly error come without giving any hint..I would be highly obliged If anyone could make me understand this kind of silly errors..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM