![](/img/trans.png)
[英]TypeError: cannot read properties of undefined (reading 'substring') in node js
[英]Getting Error [Cannot read properties of undefined (reading 'generatetypeinfo')] in Node JS API post method
我是使用 NodeJS 和 SQL 服务器进行 Restful API 开发的新手。 我正在尝试执行一个简单的 [post] 操作,我将对象数组传递给 API 端点,然后使用表值参数调用 SQL 服务器过程。 我收到以下错误
无法读取未定义的属性(读取“generateTypeInfo”)
我真的很震惊地看到在谷歌上找不到一个关于这个错误的帮助主题。 我不想为此学习 ASP.NET Core,因为 JavaScript 的学习曲线很容易。 Rest API NodeJS 和 SQL Server 组合开发是不是我做错了? 下面是我在 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;
生成表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;
};
我现在找到了解决方案。 实际上,如果您可以看到 generateTable function 的代码,我是将列添加到表中,但没有提及导致此错误的列的数据类型。 我在 [colOptions] object 中添加了一个属性 [type],并传递给 function [Generatetable] 中的 columns.add 命令。 无论如何,非常感谢戴尔的快速回复。 K.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.