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 create property 'hooks' on string ???

See original GitHub issue

During 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:closed
  • Created 4 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
SimonSchickcommented, Apr 5, 2019

Oh, you can’t pass a string as the second argument to belongsTo, you probably meant to use { as: <string> }.

1reaction
SimonSchickcommented, Apr 5, 2019

@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.

Read more comments on GitHub >

github_iconTop 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 >

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