Nodejs API批量发布,正文中包含JSON数据

时间:2017-04-21 10:19:18

标签: sql-server node.js api angular npm

我正在构建一个带有Nodejs API的Angular2应用程序,用于使用NPM包'mssql'来操作Microsoft SQL Server。 一切都很完美,但我无法理解一件事。

我必须创建一种从csv文件发布一批用户的方法吗?有谁知道我该怎么做?

提前致谢!

修改

添加了一些我已经拥有的代码(并且有效......)

组件:

`var array: Gebruiker[] = [];
array.push(model);
this.gebruikerService.insertGebruiker(array).subscribe(
...`

服务:

`insertGebruiker(data) {
    //Create headers for JSON data
    var headers = new Headers();
    headers.append('Content-Type', 'application/json');
    //create JSON from model
    var user = JSON.stringify(data);
    console.log("JSON DATA = " + data);
    return this.http.post('http://localhost:3000/api/gebruikers/post', user, {headers: headers});
  }`

API:

router.post('/gebruikers/post', (req, res) => {
sql.connect(dbconfig, function (err) {
    var request = new sql.Request();
    if (err) {
        console.log(err);
        return;
    }
    //Change variable to JSON
    var data = JSON.stringify(req.body);
    var json = JSON.parse(data);

    console.log(json)

    var count = Object.keys(json).length;

    var queryInsert = "INSERT INTO [Alg].[User] (FirstName, LastName, PrivateMail, UserName, Password, Auth, Enabled, Created, Manual, LastChanged, Staff) VALUES ";
    var queryVariables = "";
    var queryString = "";
    var counterForHowManyTimesTheQueryIsExecuted = 0;

    for (i = 0; i < count; i++) {
        //new variables for new user
        console.log(i);
        console.log(count);
        var FirstName = json[i].Firstname;
        var LastName = json[i].LastName;
        var PrivateMail = json[i].PrivateMail;
        var UserName = json[i].UserName;
        var Password = json[i].Password;
        var Staff = "1";
        queryVariables += "('" + FirstName + "', '" + LastName + "', '" + PrivateMail + "', '" + UserName + "', '" + Password + "', 'manual', 1, GetDate(), 1, GetDate(), " + Staff + ")";
        if ((i % 1000 == 0 && i != 0) || i == count - 1) {
            queryVariables += "; ";
            queryString = queryInsert + queryVariables;
            queryVariables = "";
            counterForHowManyTimesTheQueryIsExecuted++;
            console.log(queryString);
            request.query(queryString, function (err) {
                if (err) {
                    console.log(err);
                    return;
                }
                else {
                    res.end("POST request finished! ");
                }
            });
            request.query();
        }
        else {
            queryVariables += ", ";
        }
    }

    console.log(counterForHowManyTimesTheQueryIsExecuted);
});

});

1 个答案:

答案 0 :(得分:0)

使用csv-parse module。这会将CSV文本输入转换为数组或对象

以下可以是算法:

  • 上传文件
  • 使用fs模块
  • 读取文件
  • 传递给csv-parse模块。如上所述,它将返回数组数组
  • 迭代它并插入数据库

    var csvParser = require('csv-parse');
    fs.readFile(filePath, {
     encoding: 'utf-8'
     }, function(err, csvData) {
            if (err) {
               console.log(err);
             }
         csvParser(csvData, {
            delimiter: ','
           }, function(err, data) {
                  if (err) {
                        console.log(err);
                   } else {
                     // Insert into database here
                        console.log(data);
                   }
                });
           });