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❤️
JavaScript
x
65
65
1
const mysql = require("mysql2/promise")
2
const puppeteer = require('puppeteer-extra');
3
const StealthPlugin = require('puppeteer-extra-plugin-stealth')
4
puppeteer.use(StealthPlugin())
5
const AdblockerPlugin = require('puppeteer-extra-plugin-adblocker')
6
puppeteer.use(AdblockerPlugin({ blockTrackers: true }))
7
const UserAgent = require('user-agents');
8
9
(async () => {
10
for (let step = 0; step <= 5; step++) {
11
const userAgent = new UserAgent({
12
deviceCategory: "desktop"
13
});
14
const cleanUA = userAgent.toString();
15
const browser = await puppeteer.launch({
16
headless: false,
17
defaultViewport: {width: 1920, height: 1080},
18
args: ['--disable-infobars', '--user-agent=' + cleanUA]
19
});
20
const page = await browser.newPage();
21
console.log("going to the url");
22
const url = 'https://example/test?page=' + (step+1);
23
await page.goto(url);
24
await page.waitForSelector("...");
25
//clicking on all segments
26
await page.$$eval( => ..( . => .. .click()));
27
for (let i = 0; i <= 35; i++){
28
const title = await page.evaluate((i) => {
29
return ( )).innerText
30
},i);
31
console.log(title)
32
const subtitle = await page.evaluate((i) => {
33
return (document.querySelector( )).innerText
34
},i);
35
console.log(subtitle)
36
const item_price = await page.evaluate((i) => {
37
return (document.querySelector( )).innerText
38
},i);
39
console.log(item_price)
40
const ros_info = await page.evaluate((i) => {
41
return (document.querySelector( )).innerText
42
},i);
43
console.log(ros_info)
44
const img = await page.evaluate((i) => {
45
return (document.querySelector( )).src
46
},i);
47
console.log(img)
48
const p = await page.evaluate((i) => {
49
return (document.querySelector( )).innerText
50
},i);
51
console.log(p)
52
const info_items = await page.evaluate((i) => {
53
return (document.querySelector( )).innerText
54
},i);
55
console.log(info_items)
56
const link = await page.evaluate((i) => {
57
return (document.querySelector( )).href
58
},i);
59
console.log(link)
60
const theopeninfo2 = await page.evaluate((i) => {
61
return Array.from( )).map( x=> x.innerText).join("n")
62
},i);
63
console.log(theopeninfo2)
64
65
The interesting part
JavaScript
1
23
23
1
const connection = mysql.createConnection({
2
host: "localhost",
3
port: 3306,
4
user: "...",
5
password: "...",
6
database: "..."
7
});
8
9
10
connection.query(
11
"INSERT INTO `b_info`(`title`, `subtitle`, `item_price`, `ros_info`, `img`, `p`, `info_items`, `link`, `theopeninfo2`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
12
[title , subtitle , item_price , ros_info , img , p , info_items , link , theopeninfo2 ],
13
14
function(err, results) {
15
console.log(results);})
16
17
}
18
await browser.close();
19
20
}
21
})();
22
23
Advertisement
Answer
I found a solution
JavaScript
1
22
22
1
connect()
2
async function connect() {
3
try {
4
const con = await mysql.createConnection({
5
"host": ,
6
"port": ,
7
"user": ,
8
"password" : ,
9
"database" :
10
})
11
12
const insertsql = await con.query(
13
"INSERT INTO `b_info`(`title`, `subtitle`, `item_price`, `ros_info`, `img`, `p`, `info_items`, `link`, `theopeninfo2`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
14
[title , subtitle , item_price , ros_info , img , p , info_items , link , theopeninfo2 ]
15
)
16
console.table(insertsql[0])
17
}
18
catch(ex)
19
{
20
console.error(ex)
21
}
22