简体   繁体   中英

Getting Error [Cannot read properties of undefined (reading 'generatetypeinfo')] in Node JS API post method

I am new to Restful API development using NodeJS and SQL Server. I am trying to do a simple [post] operation where I am passing an array of objects to the API endpoint and then calling a SQL Server procedure with a table valued parameter. I am getting the below error

Cannot read properties of undefined (reading 'generateTypeInfo')

I was really shocked to see that there is not a single help topic found over Google regarding this error. I do not want to learn ASP.NET Core for this because JavaScript has an easy learning curve. Am I doing a mistake by developing a Rest API by using the combination of NodeJS and SQL Server? Below is my Related.JS file called in Post endpoint

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

const fn_CreateProd = async function (product) {
  let errmsg = "";
  let connPool = null;
  await sql
    .connect(global.config)
    .then((pool) => {

      global.connPool = pool;
      result = pool.request().query("select * from products where 1=2");

      return result;
    })
    .then((retResult) => {
      const srcTable = retResult.recordset.toTable("tvp_products");

      let newsrcTable = Array.from(srcTable.columns);
      
      console.log('Source table b4 mapping',srcTable)
      newsrcTable = newsrcTable.map((i) => {
        i.name = i.name.toUpperCase();
        return i;
      });
      console.log('Source table after convert array with mapping',newsrcTable)

      const prdTable = dataAccess.generateTable(
        newsrcTable,
        product,
        "tvp_products"
      );

      console.log("Prepared TVp data", prdTable);
      const newResult = dataAccess.execute(`sp3s_ins_products_tvp`, [
        { name: "tblprods", value: prdTable },
      ]);

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

module.exports = fn_CreateProd;

and Content of Generatetable function are as below:

const generateTable = (columns, entities,tvpName) => {
    const table = new mssql.Table(tvpName);
    
    // const testobj = {type : [sql.numeric],name : 'Sanjay'}
    // console.log('Columns  testobj',testobj.type)

    columns.forEach(column => {
        //  console.log('COlumn data for COlumn :',column)
        if (column && typeof column === 'object' && column.name && column.type) {
            let colOptions =  {}
            if (column.type==mssql.Numeric)
            {
               colOptions.scale=column.scale
               colOptions.precision=column.precision
            }
            else
            if (column.type==mssql.VarChar || column.type==mssql.Char )
            {
                colOptions.length = column.length
            }
            
            // console.log (`Column name type for Colummn :${column.name} -${colType}-Actual :${column['type']}`)
            if (column.hasOwnProperty('options')) {
                table.columns.add(column.name.toUpperCase(), colType,column.options);
            } else {
                table.columns.add(column.name.toUpperCase(),colOptions)

            }
        }
    });

    console.log('Generated table',table)

    const newEntities = entities.map(obj=>keystoUppercase(obj))

    // console.log('New entities after uppercase',newEntities)

    newEntities.forEach(entity => {
        table.rows.add(...columns.map(i => 
            entity[i.name]));
    });

    return table;
};

I have found the solution now. Actually, if you can see the code of generateTable function, I was adding the columns into the table but not mentioning the data type of the columns due to which this error was coming. I have added one more property [type] in the [colOptions] object being passed to columns.add command in the function [Generatetable]. Thanks a lot anyway to you for quick replies by Dale. K.

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