TypeError: Cannot read property 'replace' of undefined (when using limit without order or primary key)
See original GitHub issueThis 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:
- Created 8 years ago
- Comments:5 (2 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Works in
5.0.0-beta.11If 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.