gpt4 book ai didi

node.js - 使用 sequelize 时如何解决 SequelizeDatabaseError NaN?

转载 作者:行者123 更新时间:2023-12-03 22:23:40 25 4
gpt4 key购买 nike

为我的模型运行简单的 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'
});
});
});

最佳答案

使用如下内容将 req.params.page 转换为 number:在减法和乘法之前使用 +req.params.page。默认情况下 params 对象包含 string 值 ( API )。

关于node.js - 使用 sequelize 时如何解决 SequelizeDatabaseError NaN?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50473391/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com