【发布时间】:2017-09-25 13:23:15
【问题描述】:
我正在尝试从 csv 文件同时在我的 MSSQL Server 中插入大约 20.000 个用户。我为我的 Angular2 应用程序创建了一个 API,并将 csv 文件解析为 JSON 格式。但是现在我有点卡住了,我从 2 岁多的孩子那里找到了答案,所以这不再起作用了。有什么帮助吗?
这是我已经在使用的代码:
//Insert gebruikers from CSV file
router.post('/gebruikers/csv', type, (req, res) => {
fs.readFile(req.file.path, 'utf8', function (err, csvData) {
if (err) {
res.send("error in file reader: " + err);
return console.log(err);
}
csvParser(csvData, {
delimiter: ',',
columns: true
}, function (err, json) {
if (err) {
res.send("error in csvParser: " + err);
console.log(err);
} else {
console.log(json);
//I think the query should be done here...
}
})
})
});
提前致谢!!
编辑
现在使用此代码,但它返回此错误:
TypeError: parameter.value.getTime is not a function
代码:
router.post('/gebruikers/csv', type, (req, res) => {
fs.readFile(req.file.path, 'utf8', function (err, csvData) {
if (err) {
res.send("error in file reader: " + err);
return console.log(err);
}
csvParser(csvData, {
columns: true,
ltrim: true,
rtrim: true
}, function (err, json) {
if (err) {
res.send("error in csvParser: " + err);
console.log(err);
} else {
console.log(json);
const table = new sql.Table('[Alg].[User]');
table.create = true;
table.columns.add('Firstname', sql.NVarChar, { nullable: false });
table.columns.add('LastName', sql.NVarChar, { nullable: false });
table.columns.add('PrivateMail', sql.NVarChar, { nullable: false });
table.columns.add('UserName', sql.NVarChar, { nullable: false });
table.columns.add('Password', sql.NVarChar, { nullable: false });
table.columns.add('Auth', sql.NVarChar, { nullable: false });
table.columns.add('Enabled', sql.Bit, { nullable: false });
table.columns.add('Created', sql.SmallDateTime, { nullable: false });
table.columns.add('Manual', sql.Bit, { nullable: false });
table.columns.add('LastChanged', sql.SmallDateTime, { nullable: false });
table.columns.add('Staff', sql.Bit, { nullable: false });
var count = Object.keys(json).length;
console.log(count);
for (i = 0; i < count; i++) {
table.rows.add(json[i].Firstname, json[i].LastName, json[i].PrivateMail, json[i].UserName, json[i].Password, 'manual', 1, "GetDate()", 1, "GetDate()", 1);
}
console.log("Async function started!");
const request = new sql.Request();
request.bulk(table, (err, result) => {
// error checks?
if (err) {
console.log("ERROR post bulk gebruikers: " + err);
}
else {
res.send("SUCCES! " + result);
}
})
}
})
})
});
已修复
正如用户 Grigoriy Chudnov 指出的那样,我需要一个 new Date() 的实例。我在想这行不通,因为它的格式错误,但是 node-mssql 为我处理了这个。
currentDateTime = new Date();
table.rows.add(json[i].Firstname, json[i].LastName, json[i].PrivateMail, json[i].UserName, json[i].Password, 'manual', 1, currentDateTime, 1, currentDateTime, 1);
【问题讨论】:
-
你为
sql.SmallDateTime数据类型提供一个字符串:"GetDate()",但必须有Date的实例,例如:new Date() -
但是 GetDate() 是一个必须在 SQL 服务器本身上运行的函数?就像在查询中一样,您可以将 GetDate() 作为变量,它将当前日期放在表中?这就是我必须达到的目标。有什么想法吗?
-
如果您不想提供
new Date(),您可以在架构中设置默认值,例如:CREATE TABLE ...... DEFAULT GETDATE()或编写原始 SQL 查询。 -
不,您提供的字符串不会在服务器上运行。它由 node-mssql 库处理。
-
天哪,我一直认为 Date() 实例的格式不正确……非常抱歉!非常感谢您的帮助!!!
标签: javascript node.js api node-mssql