简体   繁体   English

在 Node JS API post 方法中出现错误 [无法读取未定义的属性(读取'generatetypeinfo')]

[英]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.我是使用 NodeJS 和 SQL 服务器进行 Restful API 开发的新手。 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.我正在尝试执行一个简单的 [post] 操作,我将对象数组传递给 API 端点,然后使用表值参数调用 SQL 服务器过程。 I am getting the below error我收到以下错误

Cannot read properties of undefined (reading 'generateTypeInfo')无法读取未定义的属性(读取“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.我不想为此学习 ASP.NET Core,因为 JavaScript 的学习曲线很容易。 Am I doing a mistake by developing a Rest API by using the combination of NodeJS and SQL Server? Rest API NodeJS 和 SQL Server 组合开发是不是我做错了? Below is my Related.JS file called in Post endpoint下面是我在 Post 端点中调用的 Related.JS 文件

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:生成表function的内容如下:

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.实际上,如果您可以看到 generateTable function 的代码,我是将列添加到表中,但没有提及导致此错误的列的数据类型。 I have added one more property [type] in the [colOptions] object being passed to columns.add command in the function [Generatetable].我在 [colOptions] object 中添加了一个属性 [type],并传递给 function [Generatetable] 中的 columns.add 命令。 Thanks a lot anyway to you for quick replies by Dale.无论如何,非常感谢戴尔的快速回复。 K. K.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型错误:无法读取节点 js 中未定义的属性(读取“子字符串”) - TypeError: cannot read properties of undefined (reading 'substring') in node js 从反应和节点js中的API检索数据时出现问题:TypeError:无法读取未定义的属性(读取'0') - Issue when retrieving data from an API in react and node js : TypeError: Cannot read properties of undefined (reading '0') Hubspot 时间轴事件 API Node.JS“TypeError:无法读取未定义的属性(读取‘eventsApi’)” - Hubspot Timeline event API Node.JS "TypeError: Cannot read properties of undefined (reading 'eventsApi')" 类型错误:无法读取未定义的属性(读取“findOne”)node.js gridFS - TypeError: Cannot read properties of undefined (reading 'findOne') node js gridFS Passport.js GoogleStrategy 不工作! 出现错误“无法读取未定义的属性(读取‘0’)” - Passport.js GoogleStrategy not working! Getting error "Cannot read properties of undefined (reading '0')" Shopify API 节点/快递无法读取未定义的属性(读取“休息”) - Shopify API Node/Express Cannot read properties of undefined (reading 'Rest') 出现错误:无法读取未定义的属性(读取“id”) - Getting error: Cannot read properties of undefined (reading 'id') 收到错误“无法读取未定义的属性(读取'id')” - Getting an error "Cannot read properties of undefined (reading 'id') " discord.js 错误:无法读取未定义的属性(读取“客户端”) - discord.js error: Cannot read properties of undefined (reading 'client') node.js/express.js 连接池无法读取未定义的属性(读取“getConnection”) - node.js/express.js connection pool Cannot read properties of undefined (reading 'getConnection')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM