简体   繁体   English

node.js将json数组对象插入mysql表

[英]node.js insert json array object into mysql table

i have a csv that I want to save into my mySQL Table. 我有一个要保存到mySQL表中的csv。 My parser works well and it also save the created json Array to my table. 我的解析器运行良好,并且还将创建的json数组保存到我的表中。 My problem is that he insert row for row in the background and don't response it. 我的问题是他在后台逐行插入并且没有响应。

My code looks like that: 我的代码如下所示:

var file= './mytable.csv';
    connection.connect(function (err) {});
var csv = require('csvtojson');

    csv({ delimiter:","})
        .fromFile(file)
        .on('end_parsed', function(jsonArray){
            for(var i = 0; i < jsonArray.length; i++){
                var post  = jsonArray[i]
                conn.query('INSERT INTO mytable SET ?', post, function(err, results) {
                    if (err) throw err;
                    console.log(result.insertId);
                });
            }
            res.end("done"); 
        })
        .on('done', function(error){
            console.log('end')
        })

My Goal is that my api send: its "done" with (res.json("Done")) when the complete query is done. 我的目标是我的api发送:完成完整的查询后,通过(res.json(“ Done”))进行“完成”。 What should I change? 我应该改变什么?

Greetings 问候

edit: my csv is realy large, with almost 500k rows! 编辑:我的csv非常大,几乎有50万行!

EDIT: 编辑:

I inserted async into my parser like that: 我将异步插入这样的解析器中:

 csv({ delimiter:";"})
    .fromFile(file)
    .on('end_parsed', function(jsonArray) {
        async.forEach(jsonArray, function (jsonArrays, callback) {
            conn.query('INSERT INTO mytable SET ?', jsonArrays, callback);
        }, function (err) {
            if (err) return next(err);
            res.json("done");
            console.log("done")
        });

    });

But he don't responde with "done" (in Terminal he write it, but postman give me only "Could not get any response") 但是他没有回复“完成”(在终端中,他写了,但是邮递员只给我“无法得到任何回复”)

Your call to res.end() / res.json() doesn't wait for all inserts to be finished. 您对res.end() / res.json()调用不会等待所有插入操作完成。

And if you start your inserts within a for-loop you start them all more or less in parallel. 而且,如果您在for循环中启动插入,则或多或少并行地启动它们。 You should take look at something like the async library ( http://caolan.github.io/async ). 您应该看看async库( http://caolan.github.io/async )之类的东西。 There you find a eachLimit() function that lets you run async operations on a collection/array. 在这里,您可以找到一个eachLimit()函数,该函数可让您在集合/数组上运行异步操作。 With this function you can limit how many operations can run in parallel. 使用此功能,您可以限制可以并行运行的操作数。 And you get a callback that is called when an error happens or all async calls have finished. 当发生错误或所有异步调用已完成时,您将获得一个回调。 Within this callback you can call the res.json(...) function to send your response. 在此回调中,您可以调用res.json(...)函数发送响应。

Sample: 样品:

var async = require('async');

//...

function save_row_to_db (post, callback) {
    conn.query('INSERT INTO mytable SET ?', post, callback);
}

function finished(err) {
    if (err) throw err;
    res.end("done");
}

async.eachLimit(csvRows, 20, save_row_to_db, finished);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM