javascriptnode.jspuppeteermysql2node-mysql2

im trying to insert data to mysql from puppeteer (async) but I getting "connection.query is not a function"


I just want to store the data I collected in the database but I getting "connection.query is not a function"

I've checked a lot of options to solve the issue and I can not find anything that works. In some of the ways the variables I collected were null and in the other part the connection.query was not a function

Please How to make it work?

Thanks a lot❤️

const mysql = require("mysql2/promise")
const puppeteer = require('puppeteer-extra');
const StealthPlugin = require('puppeteer-extra-plugin-stealth')
puppeteer.use(StealthPlugin())
const AdblockerPlugin = require('puppeteer-extra-plugin-adblocker')
puppeteer.use(AdblockerPlugin({ blockTrackers: true }))
const UserAgent = require('user-agents');

(async () => {
for (let step = 0; step <= 5; step++) {
const userAgent = new UserAgent({
   deviceCategory: "desktop"
 });
const cleanUA = userAgent.toString();
  const browser = await puppeteer.launch({
    headless: false,
    defaultViewport: {width: 1920, height: 1080},
    args: ['--disable-infobars', '--user-agent=' + cleanUA]
  });
  const page = await browser.newPage();
  console.log("going to the url");
  const url = 'https://example/test?page=' + (step+1);
  await page.goto(url);
  await page.waitForSelector("...");
  //clicking on all segments
  await page.$$eval(... => ..(.... => .. .click()));
  for (let i = 0; i <= 35; i++){
    const title = await page.evaluate((i) => {
      return (...)).innerText
  },i); 
  console.log(title)
    const subtitle = await page.evaluate((i) => {
      return (document.querySelector(...)).innerText
  },i); 
  console.log(subtitle)
    const item_price = await page.evaluate((i) => {
      return (document.querySelector(...)).innerText
  },i); 
  console.log(item_price)
    const ros_info = await page.evaluate((i) => {
      return (document.querySelector(...)).innerText
  },i); 
  console.log(ros_info)
    const img = await page.evaluate((i) => {
      return (document.querySelector(...)).src
  },i); 
  console.log(img)
    const p = await page.evaluate((i) => {
      return (document.querySelector(...)).innerText
  },i); 
  console.log(p)
    const info_items = await page.evaluate((i) => {
      return (document.querySelector(...)).innerText
  },i); 
  console.log(info_items)
    const link = await page.evaluate((i) => {
      return (document.querySelector(...)).href
  },i); 
  console.log(link)
    const theopeninfo2 = await page.evaluate((i) => {
      return Array.from(...)).map( x=> x.innerText).join("\n")
  },i);
  console.log(theopeninfo2)

The interesting part

const connection = mysql.createConnection({
  host: "localhost",
  port: 3306,
  user: "...",
  password: "...",
  database: "..."
});


  connection.query(
      "INSERT INTO `b_info`(`title`, `subtitle`, `item_price`, `ros_info`, `img`, `p`, `info_items`, `link`, `theopeninfo2`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
      [title , subtitle , item_price , ros_info , img , p , info_items , link , theopeninfo2 ],

  function(err, results) {
    console.log(results);})

  }
  await browser.close();

}
})();


Solution

  • I found a solution

    connect()
      async function connect() {
      try {
        const con = await mysql.createConnection({
          "host": ,
          "port": ,
          "user": ,
          "password" : ,
          "database" : 
        })
    
        const insertsql = await con.query(
          "INSERT INTO `b_info`(`title`, `subtitle`, `item_price`, `ros_info`, `img`, `p`, `info_items`, `link`, `theopeninfo2`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
          [title , subtitle , item_price , ros_info , img , p , info_items , link , theopeninfo2 ]
        )
        console.table(insertsql[0])
      }
      catch(ex)
      {
        console.error(ex)
      }