question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeError: Cannot read property 'replace' of undefined (when using limit without order or primary key)

See original GitHub issue

This is not necessarily a bug, just a request for better error messages.

Example: You have a model with no primary key, and you use findAll() with “limit” but no “order”:

sequalize.table.findAll({
    limit: 100
})

You get this error:

TypeError: Cannot read property 'replace' of undefined
    at Object.QueryGenerator.quoteIdentifier (PROJECT_ROOT\node_modules\sequelize\lib\dialects\mssql\query-generator.js:494:30)
    at Object.QueryGenerator.addLimitAndOffset (PROJECT_ROOT\node_modules\sequelize\lib\dialects\mssql\query-generator.js:592:26)
    at Object.QueryGenerator.selectQuery (PROJECT_ROOT\node_modules\sequelize\lib\dialects\abstract\query-generator.js:1473:27)
    at QueryInterface.select (PROJECT_ROOT\node_modules\sequelize\lib\query-interface.js:674:25)
    at null.<anonymous> (PROJECT_ROOT\node_modules\sequelize\lib\model.js:1330:32)
    at tryCatcher (PROJECT_ROOT\node_modules\bluebird\js\main\util.js:26:23)
    at Promise._settlePromiseFromHandler (PROJECT_ROOT\node_modules\bluebird\js\main\promise.js:503:31)
    at Promise._settlePromiseAt (PROJECT_ROOT\node_modules\bluebird\js\main\promise.js:577:18)
    at Async._drainQueue (PROJECT_ROOT\node_modules\bluebird\js\main\async.js:128:12)
    at Async._drainQueues (PROJECT_ROOT\node_modules\bluebird\js\main\async.js:133:10)
    at Immediate.Async.drainQueues [as _onImmediate] (PROJECT_ROOT\node_modules\bluebird\js\main\async.js:15:14)
    at processImmediate [as _immediateCallback] (timers.js:358:17)

The code is here:

if (options.limit || options.offset) {
  if (!options.order || (options.include && !subQueryOrder.length)) {
    fragment += (options.order && !isSubQuery) ? ', ' : ' ORDER BY ';
    fragment += this.quoteIdentifier(model.primaryKeyAttribute);
  }
  ...

It would be nicer to have an error that you need to specify either order or have a primary key. A simple assert or “if () throw new Error()” would be fine.

Again - not necessarily a bug, since you need to have order-by in order to use limit, but a confusing error message.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
sushantdhimancommented, Aug 28, 2018

Works in 5.0.0-beta.11

const Sequelize = require('./index');
const sequelize = require('./test/support').createSequelizeInstance();

const History = sequelize.define('history', {
  amount: Sequelize.INTEGER
});

History.removeAttribute('id');


(async () => {
  await sequelize.sync({ force: true });

  const r = await History.findAll({
    limit: 100
  });

  console.log(r);
})()
Executing (default): DROP TABLE IF EXISTS `histories`;
Executing (default): DROP TABLE IF EXISTS `histories`;
Executing (default): CREATE TABLE IF NOT EXISTS `histories` (`amount` INTEGER, `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL) ENGINE=InnoDB;
Executing (default): SHOW INDEX FROM `histories`
Executing (default): SELECT `amount`, `createdAt`, `updatedAt` FROM `histories` AS `history` LIMIT 100;
[]
0reactions
manast-apsiscommented, Apr 29, 2020

If you get an unhandled exception it is a bug… Seems like in current versions this error is still possible, unfortunately on a big codebase with many models, finding which one causes this exception is like finding a needle in a haystack.

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeError: Cannot read property 'replace' of undefined
You are calling replace() on a variable that is undefined. ... Why is range undefined? Apparently this header is not in the request....
Read more >
Cannot Read Property of Undefined in JavaScript - Rollbar
TypeError : Cannot read property of undefined occurs when a property is read or a function is called on an undefined variable.
Read more >
sequelize/sequelize - Gitter
Hi guys. I want to create a table which has the fields touser and fromuser and each field should reference the table user...
Read more >
error: cannot read property 'replace' of undefined sequelize
If you replace .done with .then it should work as expected. Open side panel. Sequelize TypeError: Cannot read property 'push' of undefined.
Read more >
Build a Secure Node.js App with SQL Server - Okta Developer
Set Up the SQL Database. You will need a SQL database to for this tutorial. If you are running SQL Server locally and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found