Cioccoluna
Cioccoluna

Reputation: 127

SQL Syntax Error in Pagination with Sequelize, but works on first page?

I'm new at Developing and have had many many problems that I solved by googleing soo much. But this one seems to be so stupid that I can't even find anyone with the same problem... Maybe you can see the error?

I'm using NodeJS with Sequelize and a MySQL database. I'm bulding a website for a job market and want to paginate the jobs. I have 6 jobs in my database right now.

//PAGINATION Function
const paginate = ({ page, pageSize }) => {
    const offset = page * pageSize;
    const limit = offset + pageSize;

    return {
        offset,
        limit
    };
};


// JOBSPAGE /jobs
exports.jobs = function (req, res) {
    let page = 1;
    let pageSize = 2;

    Job.findAll({ offset: page, limit: pageSize })
    .then(jobs => {
        paginate({page, pageSize}),
        res.render("admin/jobs", {jobs, page, pageSize});
    })
    .catch(err => {
        console.log(err);
        res.redirect("/admin");
    });
};

The thing is, my code works for the first page, when req.query.page === undefined. But when I change the url from /jobs to /jobs?page=1 I get this from my console:

Executing (default): 
SELECT
    `id`,
    `title`,
    `description`,
    `location`,
    `createdAt`,
    `updatedAt`
FROM
    `jobs` AS `job`
LIMIT '1', 2;
{ SequelizeDatabaseError: You have an error in your SQL syntax;
check the manual that corresponds to your MariaDB server version
for the right syntax to use near ''1', 2' at line 1

What am I missing here? Your help is very appreciated!

Upvotes: 0

Views: 3106

Answers (1)

Ellebkey
Ellebkey

Reputation: 2301

LIMIT receives an integer as value, If you check on the error, the first value is a string '1' I guess is the value directly from req.query.page just add + before for numerical value +req.query.page .

Upvotes: 2

Related Questions