Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot read property 'scope' of undefined #273

Closed
ramsestom opened this Issue Aug 18, 2016 · 10 comments

Comments

Projects
None yet
3 participants
@ramsestom
Copy link

ramsestom commented Aug 18, 2016

With the default configuration and a mongoDB database for the service, each time I try to register a new user (with a REST request), I have this error that is returned:

error: Route: /users - Cannot read property 'scope' of undefined
info: TypeError: Cannot read property 'scope' of undefined
    at model.Object.defineProperty.set [as password] (C:\Development\feathers\prints-server\node_modules\mongoose\lib\document.js:1712:38)
    at removeField (C:\Development\feathers\prints-server\node_modules\feathers-hooks\lib\bundled.js:216:22)
    at removeFields (C:\Development\feathers\prints-server\node_modules\feathers-hooks\lib\bundled.js:235:9)
    at next (C:\Development\feathers\prints-server\node_modules\feathers-hooks\lib\bundled.js:264:11)
    at Object.<anonymous> (C:\Development\feathers\prints-server\node_modules\feathers-hooks\lib\bundled.js:280:75)
    at run (C:\Development\feathers\prints-server\node_modules\core-js\modules\es6.promise.js:87:22)
    at C:\Development\feathers\prints-server\node_modules\core-js\modules\es6.promise.js:100:28
    at flush (C:\Development\feathers\prints-server\node_modules\core-js\modules\_microtask.js:18:9)
    at _combinedTickCallback (internal/process/next_tick.js:67:7)
    at process._tickCallback (internal/process/next_tick.js:98:9)

the error appear after the creation of the user entry in the mongoDB database.

@daffl

This comment has been minimized.

Copy link
Member

daffl commented Aug 18, 2016

To use the bundled hooks properly with Mongoose you need to use the toObject hook.

@marshallswain

This comment has been minimized.

Copy link
Member

marshallswain commented Aug 18, 2016

Or set lean:true in your service options.

@ramsestom

This comment has been minimized.

Copy link
Author

ramsestom commented Aug 18, 2016

OK Thanks. Will try it. Thought the feathers generator would have take care of all that automatically though...

@daffl

This comment has been minimized.

Copy link
Member

daffl commented Aug 18, 2016

The new one should do that indeed.

@ramsestom

This comment has been minimized.

Copy link
Author

ramsestom commented Aug 18, 2016

Seems stupid but how do I add {lean:true} to my service options?
I changed:

const options = {
    Model: user,
    paginate: {
      default: 5,
      max: 25
    }
  };
  app.use('/users', service(options));

to

const options = {
    Model: user,
    lean:true,
    paginate: {
      default: 5,
      max: 25
    }
  };

 app.use('/users', service(options));

in my user service but it didn't fixed my problem...

or is it in my authentication service that I need to set lean:true?

By the way, is there any drawback of using lean:true (it will speed up return queries so it is a clear advantage but I can't see any drawback in the case of a feathers server (where object returned needs to plain objects and not mangoose ones anyway))

@marshallswain

This comment has been minimized.

Copy link
Member

marshallswain commented Aug 19, 2016

I have not run into any issues with using lean. I've only ever wanted the validation part of mongoose models, anyway, which generally still runs properly. It looks to me like you're setting it correctly, and you want to set it on the same service you have the removeFields hook setup on.

@ramsestom

This comment has been minimized.

Copy link
Author

ramsestom commented Aug 19, 2016

I don't know exactly on which of the 2 services the removeField hook call comes from (users or auth) as I didn't setup any personal hooks on this server yet (kept the default configuration for now).
My guess would be that it is the hooks.remove('password') after hook from the user service that throw this call but, as I said, defining lean:true in the user service options didn't fixed the issue.
So it is either that lean:true is not correctly taken into account the way I defined it or that the problem is not linked to the service returning mangoose objects...

@daffl

This comment has been minimized.

Copy link
Member

daffl commented Aug 20, 2016

lean: true should be working as you are using it. I also just made a new release with feathersjs-ecosystem/feathers-hooks#92 which should also fix your issue I think.

@daffl daffl changed the title Issue with mangoose Cannot read property 'scope' of undefined Aug 20, 2016

@ramsestom

This comment has been minimized.

Copy link
Author

ramsestom commented Aug 20, 2016

Yes the new release of feathers-hooks fixed my issue. Thanks.
Any idea why lean: true didn't fix it directly? I though lean: true allows to avoid from calling toObject as raw request objects would be returned rather than mangoose ones...

@daffl

This comment has been minimized.

Copy link
Member

daffl commented Aug 20, 2016

It should work with lean: true as well. Maybe there is another issue. If you can reproduce it that way, could you open another issue with an example repository that shows the problem? I am going to close this since it looks like it is fixed.

@daffl daffl closed this Aug 20, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.