Nodejs Mysql Only Inserting one item












0














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










share|improve this question


















  • 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
















0














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










share|improve this question


















  • 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














0












0








0







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










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 at 20:48









Hashim Roja

256




256








  • 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




    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












1 Answer
1






active

oldest

votes


















0














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






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    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

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    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






    share|improve this answer


























      0














      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






      share|improve this answer
























        0












        0








        0






        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






        share|improve this answer












        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







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 10 at 22:47









        yeya

        502515




        502515






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            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





















































            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







            這個網誌中的熱門文章

            Xamarin.form Move up view when keyboard appear

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            Anylogic : not able to use stopDelay()