Nodejs Mysql Only Inserting one item
up vote
0
down vote
favorite
Hi i am trying to build an odd display website for soccer matches
Im trying to get data from betfair which works fine then insert it to my database
Problem is that the function is only inserting the first item, what should i do?
const request = require('request');
const cheerio = require('cheerio');
const fs = require('fs');
const util = require('util');
const appendFile = util.promisify(fs.appendFile);
const async = require("async");
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '',
database : 'betfair'
});
connection.connect(function(err) {
if (err) {
console.error('error connecting: ' + err.stack);
return;
}
console.log('connected as id ' + connection.threadId);
});
var url = 'https://www.betfair.com/sport/football';
var customHeaderRequest = request.defaults({
headers: {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36'}
})
customHeaderRequest.get(url, async function(err, resp, body){
try {
let $ = cheerio.load(body);
let links = $('.section-list .section:nth-child(2) .event-list li').toArray();
for (let link of links) {
const id = $(link).find('.event-information .avb-col-markets a:nth-child(1)').attr('href');
const idx = id.replace('/sport/football/event?eventId=', '');
const home = $(link).find('.event-information div:nth-child(3) div a div span.team-name:nth-child(1)').text().trim();
const away = $(link).find('.event-information div:nth-child(3) div a div span.team-name:nth-child(2)').text().trim();
const x1 = $(link).find('.event-information .avb-col-markets .market-3-runners .runner-list li:nth-child(1) a .ui-runner-price').text().trim();
const x = $(link).find('.event-information .avb-col-markets .market-3-runners .runner-list li:nth-child(2) a .ui-runner-price').text().trim();
const x2 = $(link).find('.event-information .avb-col-markets .market-3-runners .runner-list li:nth-child(3) a .ui-runner-price').text().trim();
const over = $(link).find('.event-information .avb-col-markets .market-2-runners .runner-list li:nth-child(1) a .ui-runner-price').text().trim();
const under = $(link).find('.event-information .avb-col-markets .market-2-runners .runner-list li:nth-child(2) a .ui-runner-price').text().trim();
if (home && away) {
connection.query("INSERT INTO bf (id, home, away, x1, x, x2, over, under) VALUES ('"+idx+"', '"+home+"', '"+away+"', '"+x1+"', '"+x+"', '"+x2+"', '"+over+"', '"+under+"')", function (err, result) {
if (err) throw err;
});
}
}
} catch(e) {
}
});
I have tried plenty of thinks but im not that good in nodejs and i am looking for your help
Thanks
mysql node.js
add a comment |
up vote
0
down vote
favorite
Hi i am trying to build an odd display website for soccer matches
Im trying to get data from betfair which works fine then insert it to my database
Problem is that the function is only inserting the first item, what should i do?
const request = require('request');
const cheerio = require('cheerio');
const fs = require('fs');
const util = require('util');
const appendFile = util.promisify(fs.appendFile);
const async = require("async");
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '',
database : 'betfair'
});
connection.connect(function(err) {
if (err) {
console.error('error connecting: ' + err.stack);
return;
}
console.log('connected as id ' + connection.threadId);
});
var url = 'https://www.betfair.com/sport/football';
var customHeaderRequest = request.defaults({
headers: {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36'}
})
customHeaderRequest.get(url, async function(err, resp, body){
try {
let $ = cheerio.load(body);
let links = $('.section-list .section:nth-child(2) .event-list li').toArray();
for (let link of links) {
const id = $(link).find('.event-information .avb-col-markets a:nth-child(1)').attr('href');
const idx = id.replace('/sport/football/event?eventId=', '');
const home = $(link).find('.event-information div:nth-child(3) div a div span.team-name:nth-child(1)').text().trim();
const away = $(link).find('.event-information div:nth-child(3) div a div span.team-name:nth-child(2)').text().trim();
const x1 = $(link).find('.event-information .avb-col-markets .market-3-runners .runner-list li:nth-child(1) a .ui-runner-price').text().trim();
const x = $(link).find('.event-information .avb-col-markets .market-3-runners .runner-list li:nth-child(2) a .ui-runner-price').text().trim();
const x2 = $(link).find('.event-information .avb-col-markets .market-3-runners .runner-list li:nth-child(3) a .ui-runner-price').text().trim();
const over = $(link).find('.event-information .avb-col-markets .market-2-runners .runner-list li:nth-child(1) a .ui-runner-price').text().trim();
const under = $(link).find('.event-information .avb-col-markets .market-2-runners .runner-list li:nth-child(2) a .ui-runner-price').text().trim();
if (home && away) {
connection.query("INSERT INTO bf (id, home, away, x1, x, x2, over, under) VALUES ('"+idx+"', '"+home+"', '"+away+"', '"+x1+"', '"+x+"', '"+x2+"', '"+over+"', '"+under+"')", function (err, result) {
if (err) throw err;
});
}
}
} catch(e) {
}
});
I have tried plenty of thinks but im not that good in nodejs and i am looking for your help
Thanks
mysql node.js
1
You need to log the errors - it should help you find the issue. change the:} catch(e) { }
to} catch(e) { console.log(e); }
– yeya
Nov 10 at 22:28
@yeya dont know why but the id.replace throws error after inserting first item. I removed it and works like a charm now thank you
– Hashim Roja
Nov 10 at 22:33
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Hi i am trying to build an odd display website for soccer matches
Im trying to get data from betfair which works fine then insert it to my database
Problem is that the function is only inserting the first item, what should i do?
const request = require('request');
const cheerio = require('cheerio');
const fs = require('fs');
const util = require('util');
const appendFile = util.promisify(fs.appendFile);
const async = require("async");
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '',
database : 'betfair'
});
connection.connect(function(err) {
if (err) {
console.error('error connecting: ' + err.stack);
return;
}
console.log('connected as id ' + connection.threadId);
});
var url = 'https://www.betfair.com/sport/football';
var customHeaderRequest = request.defaults({
headers: {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36'}
})
customHeaderRequest.get(url, async function(err, resp, body){
try {
let $ = cheerio.load(body);
let links = $('.section-list .section:nth-child(2) .event-list li').toArray();
for (let link of links) {
const id = $(link).find('.event-information .avb-col-markets a:nth-child(1)').attr('href');
const idx = id.replace('/sport/football/event?eventId=', '');
const home = $(link).find('.event-information div:nth-child(3) div a div span.team-name:nth-child(1)').text().trim();
const away = $(link).find('.event-information div:nth-child(3) div a div span.team-name:nth-child(2)').text().trim();
const x1 = $(link).find('.event-information .avb-col-markets .market-3-runners .runner-list li:nth-child(1) a .ui-runner-price').text().trim();
const x = $(link).find('.event-information .avb-col-markets .market-3-runners .runner-list li:nth-child(2) a .ui-runner-price').text().trim();
const x2 = $(link).find('.event-information .avb-col-markets .market-3-runners .runner-list li:nth-child(3) a .ui-runner-price').text().trim();
const over = $(link).find('.event-information .avb-col-markets .market-2-runners .runner-list li:nth-child(1) a .ui-runner-price').text().trim();
const under = $(link).find('.event-information .avb-col-markets .market-2-runners .runner-list li:nth-child(2) a .ui-runner-price').text().trim();
if (home && away) {
connection.query("INSERT INTO bf (id, home, away, x1, x, x2, over, under) VALUES ('"+idx+"', '"+home+"', '"+away+"', '"+x1+"', '"+x+"', '"+x2+"', '"+over+"', '"+under+"')", function (err, result) {
if (err) throw err;
});
}
}
} catch(e) {
}
});
I have tried plenty of thinks but im not that good in nodejs and i am looking for your help
Thanks
mysql node.js
Hi i am trying to build an odd display website for soccer matches
Im trying to get data from betfair which works fine then insert it to my database
Problem is that the function is only inserting the first item, what should i do?
const request = require('request');
const cheerio = require('cheerio');
const fs = require('fs');
const util = require('util');
const appendFile = util.promisify(fs.appendFile);
const async = require("async");
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '',
database : 'betfair'
});
connection.connect(function(err) {
if (err) {
console.error('error connecting: ' + err.stack);
return;
}
console.log('connected as id ' + connection.threadId);
});
var url = 'https://www.betfair.com/sport/football';
var customHeaderRequest = request.defaults({
headers: {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36'}
})
customHeaderRequest.get(url, async function(err, resp, body){
try {
let $ = cheerio.load(body);
let links = $('.section-list .section:nth-child(2) .event-list li').toArray();
for (let link of links) {
const id = $(link).find('.event-information .avb-col-markets a:nth-child(1)').attr('href');
const idx = id.replace('/sport/football/event?eventId=', '');
const home = $(link).find('.event-information div:nth-child(3) div a div span.team-name:nth-child(1)').text().trim();
const away = $(link).find('.event-information div:nth-child(3) div a div span.team-name:nth-child(2)').text().trim();
const x1 = $(link).find('.event-information .avb-col-markets .market-3-runners .runner-list li:nth-child(1) a .ui-runner-price').text().trim();
const x = $(link).find('.event-information .avb-col-markets .market-3-runners .runner-list li:nth-child(2) a .ui-runner-price').text().trim();
const x2 = $(link).find('.event-information .avb-col-markets .market-3-runners .runner-list li:nth-child(3) a .ui-runner-price').text().trim();
const over = $(link).find('.event-information .avb-col-markets .market-2-runners .runner-list li:nth-child(1) a .ui-runner-price').text().trim();
const under = $(link).find('.event-information .avb-col-markets .market-2-runners .runner-list li:nth-child(2) a .ui-runner-price').text().trim();
if (home && away) {
connection.query("INSERT INTO bf (id, home, away, x1, x, x2, over, under) VALUES ('"+idx+"', '"+home+"', '"+away+"', '"+x1+"', '"+x+"', '"+x2+"', '"+over+"', '"+under+"')", function (err, result) {
if (err) throw err;
});
}
}
} catch(e) {
}
});
I have tried plenty of thinks but im not that good in nodejs and i am looking for your help
Thanks
mysql node.js
mysql node.js
asked Nov 10 at 20:48
Hashim Roja
276
276
1
You need to log the errors - it should help you find the issue. change the:} catch(e) { }
to} catch(e) { console.log(e); }
– yeya
Nov 10 at 22:28
@yeya dont know why but the id.replace throws error after inserting first item. I removed it and works like a charm now thank you
– Hashim Roja
Nov 10 at 22:33
add a comment |
1
You need to log the errors - it should help you find the issue. change the:} catch(e) { }
to} catch(e) { console.log(e); }
– yeya
Nov 10 at 22:28
@yeya dont know why but the id.replace throws error after inserting first item. I removed it and works like a charm now thank you
– Hashim Roja
Nov 10 at 22:33
1
1
You need to log the errors - it should help you find the issue. change the:
} catch(e) { }
to } catch(e) { console.log(e); }
– yeya
Nov 10 at 22:28
You need to log the errors - it should help you find the issue. change the:
} catch(e) { }
to } catch(e) { console.log(e); }
– yeya
Nov 10 at 22:28
@yeya dont know why but the id.replace throws error after inserting first item. I removed it and works like a charm now thank you
– Hashim Roja
Nov 10 at 22:33
@yeya dont know why but the id.replace throws error after inserting first item. I removed it and works like a charm now thank you
– Hashim Roja
Nov 10 at 22:33
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
You didn't mention what the error is but I guess you don't really need the id so removing it will be OK.
P.S. Your code has many more issues, the most important one is the sql query.
You should use knex or at least change it to:
INSERT INTO bf (home, away, x1, x, x2, over, under) VALUES (?, ?, ?, ?, ?, ?, ?)",
[home, away, x1, x, x2, over, under], function (err, result) {
Source
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
You didn't mention what the error is but I guess you don't really need the id so removing it will be OK.
P.S. Your code has many more issues, the most important one is the sql query.
You should use knex or at least change it to:
INSERT INTO bf (home, away, x1, x, x2, over, under) VALUES (?, ?, ?, ?, ?, ?, ?)",
[home, away, x1, x, x2, over, under], function (err, result) {
Source
add a comment |
up vote
0
down vote
You didn't mention what the error is but I guess you don't really need the id so removing it will be OK.
P.S. Your code has many more issues, the most important one is the sql query.
You should use knex or at least change it to:
INSERT INTO bf (home, away, x1, x, x2, over, under) VALUES (?, ?, ?, ?, ?, ?, ?)",
[home, away, x1, x, x2, over, under], function (err, result) {
Source
add a comment |
up vote
0
down vote
up vote
0
down vote
You didn't mention what the error is but I guess you don't really need the id so removing it will be OK.
P.S. Your code has many more issues, the most important one is the sql query.
You should use knex or at least change it to:
INSERT INTO bf (home, away, x1, x, x2, over, under) VALUES (?, ?, ?, ?, ?, ?, ?)",
[home, away, x1, x, x2, over, under], function (err, result) {
Source
You didn't mention what the error is but I guess you don't really need the id so removing it will be OK.
P.S. Your code has many more issues, the most important one is the sql query.
You should use knex or at least change it to:
INSERT INTO bf (home, away, x1, x, x2, over, under) VALUES (?, ?, ?, ?, ?, ?, ?)",
[home, away, x1, x, x2, over, under], function (err, result) {
Source
answered Nov 10 at 22:47
yeya
450513
450513
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243264%2fnodejs-mysql-only-inserting-one-item%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
You need to log the errors - it should help you find the issue. change the:
} catch(e) { }
to} catch(e) { console.log(e); }
– yeya
Nov 10 at 22:28
@yeya dont know why but the id.replace throws error after inserting first item. I removed it and works like a charm now thank you
– Hashim Roja
Nov 10 at 22:33