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

v3.31.0 introduces bug #1754

Open
ataft opened this issue Jun 27, 2019 · 10 comments
Open

v3.31.0 introduces bug #1754

ataft opened this issue Jun 27, 2019 · 10 comments

Comments

@ataft
Copy link

ataft commented Jun 27, 2019

The following code works fine in v3.30.0:

const query = {
    where: {
        or: [
            { username: username },
            { email: username }
        ]
    }
};
self.userModel.findOne(query, function (err, user) {

As of v.3.31.0, I get the following error:

Error: The where clause "[email protected]" is not an object
    at Function.ModelUtils._coerce (C:\apps\flexit\node_modules\loopback-datasource-juggler\lib\model-utils.js:369:11)
    at Function.ModelUtils._coerce (C:\apps\flexit\node_modules\loopback-datasource-juggler\lib\model-utils.js:552:20)
    at Function.ModelUtils._coerce (C:\apps\flexit\node_modules\loopback-datasource-juggler\lib\model-utils.js:394:14)
    at Function.ModelUtils._normalize (C:\apps\flexit\node_modules\loopback-datasource-juggler\lib\model-utils.js:177:8)
    at Function.find (C:\apps\flexit\node_modules\loopback-datasource-juggler\lib\dao.js:1529:10)
    at Function.findOne (C:\apps\flexit\node_modules\loopback-datasource-juggler\lib\dao.js:1782:8)
    at Strategy._verify (C:\apps\flexit\server\boot\authentication.js:288:36)
    at Strategy.authenticate (C:\apps\flexit\node_modules\passport-local\lib\strategy.js:90:12)
    at attempt (C:\apps\flexit\node_modules\passport\lib\middleware\authenticate.js:361:16)
    at authenticate (C:\apps\flexit\node_modules\passport\lib\middleware\authenticate.js:362:7)

Looks like something broke in the _coerce function.

@raymondfeng
Copy link
Contributor

It seems to be related to 4f31db1.

@b-admike
Copy link
Contributor

b-admike commented Jun 27, 2019

@ataft Can you check #1755 to see if I'm missing something for a test I wrote using your scenario? I am not able to reproduce your issue with it:

biniams-mbp:loopback-datasource-juggler badmike$ npm t

> [email protected] test /Users/badmike/loopback/loopback-datasource-juggler
> nyc mocha



  ․

  1 passing (46ms)

Here is the User model definition that I used.

@b-admike
Copy link
Contributor

@ataft can you share a sandbox app to reproduce the failure if that's easier? Please also include the steps you took to see the error.

@ataft
Copy link
Author

ataft commented Jun 27, 2019

Test looks OK, but it does differ from mine in that it doesn't include "username". Perhaps if you tried:

or: [{username: '[email protected]'}, {email: '[email protected]'}],

I doubt this will make it fail though. It's probably some other difference between our environments (I'm on loopback version 3.17.1). I'll try to reproduce the issue with a sandbox app, but for now I'm just going to stick on juggler v3.30.0.

@stale
Copy link

stale bot commented Aug 26, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Aug 26, 2019
@stale
Copy link

stale bot commented Sep 9, 2019

This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository.

@stale stale bot closed this as completed Sep 9, 2019
@ataft
Copy link
Author

ataft commented Oct 29, 2019

OK, I finally figured out what was causing this error. I'm using the "defineProperty" function, like this:
app.models.User.defineProperty('email', { required:false });

In the loopback-datasource-juggler, this causes the DataType to be type: [Function: AnonymousModel_6], which makes isNestedModel true, here:
https://github.com/strongloop/loopback-datasource-juggler/blob/master/lib/model-utils.js#L544

Then, it throws an error any time "email" is used in the where clause, e.g.

    var whereClause = {
      where: {
        email: '[email protected]'
      },
    };
    app.models.User.findOne(whereClause, function (err, user) {
      console.log('err: ', err);
      console.log('user: ', user);
    });

It has nothing to do with using "OR", like I originally thought.

Here's a sandbox that shows the issue:
https://github.com/ataft/loopback-sandbox

@ataft
Copy link
Author

ataft commented Oct 29, 2019

@b-admike I see that @raymondfeng comment about it being related to 4f31db1 is spot on

@bajtos
Copy link
Member

bajtos commented Nov 1, 2019

@ataft thank you for the update. Would you like to contribute the fix yourself? I am not sure if you are using defineProperty correctly, shouldn't you specify type too?

app.models.User.defineProperty('email', {
  type: 'string',
  required: false,
});

@bajtos bajtos reopened this Nov 1, 2019
ataft added a commit to ataft/loopback-sandbox that referenced this issue Nov 1, 2019
@ataft
Copy link
Author

ataft commented Nov 1, 2019

Thanks @bajtos , adding type: 'string' fixes the problem. I tried to find where type was being set to [Function: AnonymousModel_6], but could not find this and don't know whether it's intended or not, so I'll let you decide that. I'm satisfied with just adding type: 'string'.

I'm not sure I'm doing this properly, but I want to use the built-in User model without email for auth providers like LDAP and Active Directory. Let me know if there's a better way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants