sequelize-mock icon indicating copy to clipboard operation
sequelize-mock copied to clipboard

​​TypeError: User.$useHandler is not a function​​

Open bentedder opened this issue 7 years ago • 14 comments

I feel like I'm being an idiot :), but I've setup my test as close to the docs as possible, yet I'm getting an TypeError: User.$useHandler is not a function error any time I try to run tests. I've dug through the code trying see if I'm using it at the right place and I'm stuck. Any help is appreciated, the library looks super useful!

var SequelizeMock = require('sequelize-mock');
var dbMock = new SequelizeMock();
var User = dbMock.define('User', {
  title: 'hi',
});

User.$useHandler(function(query, queryOptions, done) {
  if (query === "findOne") {
      return Promise.resolve('test');
  } else if (query === "destroy") {
      return Promise.reject(new SequelizeMock.Error("DB down"));
  } else {
      // This handler can handle this query
      return;
  }
});

bentedder avatar Dec 15 '17 10:12 bentedder

It looks like these functions are not be exposed properly on the Model prototype as we'd expect.

As a work around you can use User.$queryInterface.$useHandler() to get to the function for now. I'll work on addressing this for the next patch.

LoveAndCoding avatar Jan 22 '18 21:01 LoveAndCoding

oh.. spent almost 4 hours till I found that issue. maybe you should update docs for now :)

idangozlan avatar Feb 08 '18 23:02 idangozlan

The same here. +1 for updating.

PaulRBerg avatar Apr 24 '18 08:04 PaulRBerg

@PaulRBerg You better use https://www.npmjs.com/package/sequelize-redis

idangozlan avatar Apr 24 '18 08:04 idangozlan

Does the solution from @ktsashes work for you?

I have : Cannot read property '$useHandler' of undefined. Any idea?

scastrec avatar Oct 29 '18 07:10 scastrec

anyone still having this issue recently? Have a mock similar to OP and both $useHandler and $queryInterface are undefined on the User.

mooniker avatar Aug 27 '19 18:08 mooniker

Got the same issue here both $useHandler and $queryInterface are undefined. version 0.10.2

BenjGue avatar Sep 24 '19 12:09 BenjGue

i am also facing the same undefined issue. any update on this?

salauddinn avatar Mar 11 '20 10:03 salauddinn

static formatDuration(durationObj) { function hours(h) { if (h === undefined){ console.log(h.hours) } return h; } var hours=10; console.log(hours(hours)); const duration = ${durationObj.hours ? (durationObj.hours + ':') : ''}${ durationObj.minutes ? durationObj.minutes : '00' }:${ (durationObj.seconds < 10) ? ('0' + durationObj.seconds) : (durationObj.seconds ? durationObj.seconds : '00') }; return duration; }

static newMethod(hours) { return new hours(); } };

Please how can I fix this?

TypeError: hours is not a function

Andrew-9 avatar Jun 27 '20 17:06 Andrew-9

This is still a problem by the way. This work around worked for me https://github.com/BlinkUX/sequelize-mock/issues/43#issuecomment-359565431, but I only found it after a few hours of scrolling through the docs and my code, wondering what was wrong. If there isn't going to be a fix soon, someone should at least update the docs temporarily.

I also had to use this workaround for $clearHandlers() and $clearResults()

JakeQuilty avatar Jul 16 '21 22:07 JakeQuilty

6 years... Is there any update? Why this bug still be there?

mrdulin avatar Mar 21 '23 05:03 mrdulin

Are you going to fix this problem?

FOX521 avatar Apr 18 '23 15:04 FOX521

I have made an update on the documentation checkout my pull requests #101

officialkevinbrian avatar Jul 21 '23 18:07 officialkevinbrian

I updated the documentation

officialkevinbrian avatar Jul 21 '23 18:07 officialkevinbrian