【发布时间】:2023-02-11 05:16:12
【问题描述】:
我是使用 NodeJS 和 SQL Server 进行 Restful API 开发的新手。我正在尝试执行一个简单的 [post] 操作,我将对象数组传递给 API 端点,然后使用表值参数调用 SQL Server 过程。我收到以下错误
无法读取未定义的属性(读取“generateTypeInfo”)
我真的很震惊地看到在谷歌上找不到一个关于这个错误的帮助主题。我不想为此学习 ASP.NET Core,因为 JavaScript 的学习曲线很容易。我通过结合使用 NodeJS 和 SQL Server 来开发 Rest API 是不是犯了一个错误?下面是我在 Post 端点中调用的相关 .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;和Generatetable函数的内容如下:
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 column :${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; };
【问题讨论】:
-
嗨戴尔,感谢您的快速回复。我刚刚通过添加参考代码再次编辑了问题
-
错误发生在哪一行?
-
它发生在执行我传递表值参数的过程时。此 tvp 的内容由传递的输入对象填充。
-
那是哪条线呢?
-
请检查这个 const newResult = dataAccess.execute(
sp3s_ins_products_tvp, [ { name: "tblprods", value: prdTable }, ]);
标签: node.js sql-server api rest