我为我的模型运行一个简单的sequelize.js查询时收到此错误。
Executing (default): CREATE TABLE IF NOT EXISTS `Books` (`id` INTEGER PRIMARY KEY, `title` VARCHAR(255), `author` VARCHAR(255), `genre` VARCHAR(255), `first_published` INTEGER);
Executing (default): PRAGMA INDEX_LIST(`Books`)
Executing (default): PRAGMA INDEX_INFO(`sqlite_autoindex_books_1`)
Executing (default): CREATE TABLE IF NOT EXISTS `Patrons` (`id` INTEGER PRIMARY KEY, `first_name` VARCHAR(255), `last_name` VARCHAR(255), `address` VARCHAR(255), `email` VARCHAR(255), `library_id` VARCHAR(255), `zip_code` INTEGER);
Executing (default): PRAGMA INDEX_LIST(`Patrons`)
Executing (default): SELECT count(*) AS `count` FROM `Books` AS `Book`;
Executing (default): SELECT `id`, `title`, `author`, `genre`, `first_published` FROM `Books` AS `Book` ORDER BY `Book`.`title` LIMIT 0, 4;
GET /1 304 410.149 ms - -
GET /stylesheets/style.css 304 1.446 ms - -
Executing (default): SELECT count(*) AS `count` FROM `Books` AS `Book`;
Executing (default): SELECT `id`, `title`, `author`, `genre`, `first_published` FROM `Books` AS `Book` ORDER BY `Book`.`title` LIMIT NaN, 4;
Unhandled rejection SequelizeDatabaseError: SQLITE_ERROR: no such column: NaN
at Query.formatError (C:\Users\user\Downloads\Sandbox\10\node_modules\sequelize\lib\dialects\sqlite\query.js:423:16)
at afterExecute (C:\Users\user\Downloads\Sandbox\10\node_modules\sequelize\lib\dialects\sqlite\query.js:119:32)
at replacement (C:\Users\user\Downloads\Sandbox\10\node_modules\sqlite3\lib\trace.js:19:31)
at Statement.errBack (C:\Users\user\Downloads\Sandbox\10\node_modules\sqlite3\lib\sqlite3.js:16:21)
有趣的是代码工作正常,只是控制台在每次路由被击中时吐出上述错误。
我基本上有一个使用快速路由器的简单路由,它接受一个id参数。然后我使用id参数计算LIMIT和OFFSET的一些值,以便在我的分页中使用。
在我看来,查询正在执行两次,第二次是偏移是NaN
首次执行:
Executing (default): SELECT `id`, `title`, `author`, `genre`, `first_published` FROM `Books` AS `Book` ORDER BY `Book`.`title` LIMIT 0, 4;
第二次执行:
Executing (default): SELECT `id`, `title`, `author`, `genre`, `first_published` FROM `Books` AS `Book` ORDER BY `Book`.`title` LIMIT NaN, 4;
这是代码:
var express = require('express');
var router = express.Router();
var Book = require('../models').Book;
router.get('/:page', function (req, res, next) {
Book.findAndCountAll({
order: [ ['title'] ],
offset: ((req.params.page - 1) * 4),
limit: 4
}).then(function (book) {
let pages = Math.ceil(book.count / 4);
res.render('index', {
content: book.rows,
pagination: pages,
title: 'Express'
});
});
});
答案 0 :(得分:1)
在减去和乘法之前,使用req.params.page
这样的内容将number
投射到+req.params.page
。默认情况下,params
对象包含string
个值(API)。