【问题标题】:How to bulk insert array of objects and insert MySQL in NodeJS如何批量插入对象数组并在 NodeJS 中插入 MySQL
【发布时间】:2020-09-02 20:26:00
【问题描述】:

我注意到这里有一些类似的问题,即问题编号:23501241, StackOverflow 上有 20122962 和 50691096,但不幸的是,没有一个能解决我的问题。

我有一个如下所示的对象数组:

 [ {
    title: '2015 Jeep Wrangler Unlimited Sport SUV 4D',
    price: '$15,998',
    location: 'Tampa, Florida',
    miles: '135K miles',
    itemURL: '/marketplace/item/656602258232102/',
    imgUrl: 'https://example.com',
    seen: 0,
    created_date: 2020-05-16T14:51:30.000Z
  }
]

我整天都在尝试将该数组插入 MySQL 数据库,但没有成功。这是我最新的代码:

const saveNewJeeps = async function (entity) {
    var con = await dbConnection();
    let objLen = entity.length;

    // FOR EACH OBJECT IN ARRAY...
    for (var i = 0; i < objLen; i++) {
        var savedJeeps = con.query('INSERT INTO newjeeps SET ?', entity[i], function (err, result) {
            // Neat!
            console.log(result);
        });  
    }

    con.release();

}

有趣的是,当我独立运行函数时,通过创建一个像上面那样的对象,并通过函数发送它......它可以工作。但在我的应用工作流程中,它失败了。

我总是遇到同样的错误,抱怨循环开始时长度未定义。

这是我的完整代码:

const puppeteer = require('puppeteer');
const jsonfile = require("jsonfile");
const _ = require("lodash");
var mysql = require('mysql');
const dbConnection = require("./dbConnection");

const getSavedItems = async function () {
    let con = await dbConnection();
    try {
        await con.query("START TRANSACTION");
        let savedItems = await con.query("SELECT * FROM jeeps");
        await con.query("COMMIT");
        //console.log(savedItems);
        return savedItems;
    } catch (ex) {
        console.log(ex);
        throw ex;
    } finally {
        await con.release();
        await con.destroy();
    }
}

const saveNewJeeps = async function (entity) {
    var con = await dbConnection();
    let objLen = entity.length;

    // FOR EACH OBJECT IN ARRAY...
    for (var i = 0; i < objLen; i++) {
        var savedJeeps = con.query('INSERT INTO newjeeps SET ?', entity[i], function (err, result) {
            // Neat!
            console.log(result);
        });  
    }

    con.release();

}

// Gets current items  Search Results
const getItems = async searchTerm => {

    browser = await puppeteer.launch({
        headless: true,
        timeout: 0,
        args: ["--no-sandbox"]
    });

    page = await browser.newPage();
    await page.goto(`https://facebook.com/marketplace/tampa/search/?query=${encodeURI(searchTerm)}&sort=created_date_descending&exact=false`);
    await autoScroll(page);

    const itemList = await page.waitForSelector('div > div > span > div > a[tabindex="0"]')
        .then(() => page.evaluate(() => {

            const itemArray = [];
            const itemNodeList = document.querySelectorAll('div > div > span > div > a[tabindex="0"]');

            itemNodeList.forEach(item => {

                const itemTitle = item.innerText;
                const itemURL = item.getAttribute('href');
                const itemImg = item.querySelector('div > div > span > div > a > div > div > div > div > div > div > img').getAttribute('src');

                var obj = ['price', 'title', 'location', 'miles',
                        ...itemTitle.split(/\n/)
                    ]
                    .reduce((a, c, i, t) => {
                        if (i < 4) a[c] = t[i + 4]
                        return a
                    }, {});

                obj.imgUrl = itemImg;
                obj.itemURL = itemURL;

                itemArray.push(obj);
            });

            return itemArray;

        }))
        .catch(() => console.log("Selector error."));

    return itemList;

}


// This takes care of the auto scrolling problem
async function autoScroll(page) {
    await page.evaluate(async () => {
        await new Promise(resolve => {
            var totalHeight = 0;
            var distance = 100;
            var timer = setInterval(() => {
                var scrollHeight = document.body.scrollHeight;
                window.scrollBy(0, distance);
                totalHeight += distance;

                if (totalHeight >= scrollHeight || scrollHeight > 9000) {
                    clearInterval(timer);
                    resolve();
                }
            }, 100);
        });
    });
}


const getDifferences = async function (objNew, objOld) {

    return _.difference(objNew, objOld);
}

const init = async function () {
    const newItems = await getItems("Jeep Wrangler");
    const oldItems = await getSavedItems();
    const finalArray = await getDifferences(newItems, oldItems);
    const saveSuccess = await saveNewJeeps(finalArray);

}


const myObj =  [ {
    title: '2015 Jeep Wrangler Unlimited Sport SUV 4D',
    price: '$15,998',
    location: 'Tampa, Florida',
    miles: '135K miles',
    itemURL: '/marketplace/item/656602258232102/',
    imgUrl: 'https://example.com',
    seen: 0
  }, 
  {
    title: '2020 BMW SUV 4D',
    price: '$55,998',
    location: 'gyyu, Florida',
    miles: '15K miles',
    itemURL: '/marketplace/item/6566102/',
    imgUrl: 'https://example2.com',
    seen: 0
  }
];

// This will work just fine.
saveNewJeeps(myObj);

// But going this way, it fails...
init();

谁能明白为什么会失败?感谢您的关注。

【问题讨论】:

    标签: javascript mysql node.js


    【解决方案1】:
        currentLogs = [
     { socket_id: 'Server', message: 'Socketio online', data: 'Port  3333', logged: '2014-05-14 14:41:11' },
     { socket_id: 'Server', message: 'Waiting for Pi to connect...', data: 'Port: 8082', logged: '2014-05-14 14:41:11' }
    ];
    
    console.warn(currentLogs.map(logs=>[ logs.socket_id , logs.message , logs.data , logs.logged ]));
    

    可以使用嵌套数组进行批量插入,请参阅github page

    嵌套数组被转换为分组列表(用于批量插入),例如 [['a', 'b'], ['c', 'd']] 变成 ('a', 'b'), ('c', 'd')

    您只需插入一个嵌套的元素数组。

    here中给出了一个例子

    var mysql = require('mysql');
    var conn = mysql.createConnection({
        ...
    });
    
    var sql = "INSERT INTO Test (name, email, n) VALUES ?";
    var values = [
        ['demian', 'demian@gmail.com', 1],
        ['john', 'john@gmail.com', 2],
        ['mark', 'mark@gmail.com', 3],
        ['pete', 'pete@gmail.com', 4]
    ];
    conn.query(sql, [values], function(err) {
        if (err) throw err;
        conn.end();
    });
    

    注意

    价值观

    是用数组包裹的数组

    [ [ [...], [...], [...] ] ]
    

    还有一个完全不同的node-msql 包用于批量插入

    希望对你有帮助:)

    here的回答

    【讨论】:

      【解决方案2】:

      我通过完全不同的路线找到了答案。不知道我是否应该删除它。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-21
        • 2021-10-25
        • 2018-05-31
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多