TypeError: Cannot create property 'hooks' on string ???
See original GitHub issueDuring fetch from User model I got this
options.hooks = options.hooks === undefined ? false : Boolean(options.hooks);
^
TypeError: Cannot create property 'hooks' on string 'food_id'
at Function.<anonymous> (C:\Projects\learnSequelize\node_modules\sequelize\lib\associations\mixin.js:98:19)
at Function.Order.associate (C:\Projects\learnSequelize\database\models\order.js:17:11)
at Object.keys.forEach.modelName (C:\Projects\learnSequelize\database\models\index.js:30:19)
at Array.forEach (<anonymous>)
at Object.<anonymous> (C:\Projects\learnSequelize\database\models\index.js:28:17)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
at Object.<anonymous> (C:\Projects\learnSequelize\app.js:1:76)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:743:3)
My User Models:
'use strict';
module.exports = (sequelize, DataTypes) => {
const User = sequelize.define('User', {
name: DataTypes.STRING,
address: DataTypes.STRING,
phone: DataTypes.STRING,
email: DataTypes.STRING,
password: DataTypes.STRING,
}, {
tableName: 'users'
});
User.associate = function(models) {
User.belongsToMany(models.Role, { through: 'user_role', foreignKey: 'user_id', otherKey: 'role_id' });
User.hasMany(models.Order, 'user_id');
User.hasMany(models.Food, 'canteen_manager_id');
};
return User;
};
Food Model:
'use strict';
module.exports = (sequelize, DataTypes) => {
const Food = sequelize.define('Food', {
title: DataTypes.STRING,
price: DataTypes.FLOAT,
quantity: DataTypes.SMALLINT,
description: DataTypes.STRING,
canteen_manager_id: DataTypes.INTEGER
}, {
tableName: 'foods'
});
Food.associate = function(models) {
Food.belongsTo(models.User, 'canteen_manager_id');
};
return Food;
};
Constraint:
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.addConstraint('foods', ['canteen_manager_id'], {
type: 'foreign key',
name: 'fk_canteen_manager_id',
references: {
table: 'users',
field: 'id'
},
onDelete: 'cascade',
onUpdate: 'cascade'
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.removeConstraint('foods', 'fk_canteen_manager_id');
}
};
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Uncaught TypeError: Cannot create property 'value' on string
This error occurs because the setState function is not being handled properly here. Unlike the setState method found in class components, ...
Read more >Cannot create property 'afterWriteTickInfo' on string '[Circular]'
TypeError : Cannot create property 'afterWriteTickInfo' on string '[Circular]'.
Read more >[Solved]-Cannot create property 'key' on string-Reactjs
I believe what you have is an immutable object which you're trying to modify. ... A DataSnapshot is an efficiently generated, immutable copy...
Read more >Cannot create property 'currency' on string 'C2' - Syncfusion
Uncaught TypeError: Cannot create property 'currency' on string 'C2' ... it seems to me the grid should have all JS hooks ready at...
Read more >TypeError: Cannot read property 'trim' of Undefined in JS
To solve the error, make sure the element at the index is available and a string.
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
Oh, you can’t pass a string as the second argument to
belongsTo
, you probably meant to use{ as: <string> }
.@rabin999 you are free to create a PR to improve the documentation as you see fit, PRs are welcome. I also suggest you may switch to TS as it will help you avoid these types of simple mistakes from the get go.