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

Using near filter in where query cause toEntity(model) bug #1419

Closed
hosiduy opened this issue Jun 12, 2018 · 9 comments · Fixed by loopbackio/loopback-datasource-juggler#1690

Comments

@hosiduy
Copy link
Contributor

hosiduy commented Jun 12, 2018

Description

I have Address model:

Address extends Entity {

@property({
    type: 'number',
    id: true,
    generated: true,
    mysql: {
        "columnName": "m_id"
    }
})
mId?: number;

@property({
    type: 'number',
    mysql: {
        "columnName": "m_belong_to_id"
    }
})
mBelongToId?: number;

@property({
    type: 'GeoPoint',
    required: true,
    mysql: {
        "columnName": "m_geo_point"
    }
})
mGeoPoint: GeoPoint;

}

Current Behavior

I try this query:
let nearByAddressList = await this.addressRepo.find({

            where: {
                and: [
                    {
                        mGeoPoint: {
                            near: location,
                            maxDistance: this.maxDistance,
                            unit: this.unit
                        }
                    }
                ]
            }
        });

and the console show me the bug: model.toObject() is not a function on file node_modules/@loopback/repository/dist8/src/repositories/legacy-juggler-bridge.js:160
If I remove the where filter, the code run normally.

I checked the code and couldn't find near properties on Where filter class.
Can I use near in where filter on Lp4 now?

Expected Behavior

Query run normally with near in query.

See Reporting Issues for more tips on writing good issues

@hosiduy hosiduy changed the title How to use near in where filter? Using near filter in where query cause toEntiry(model) bug Jun 14, 2018
@shimks
Copy link
Contributor

shimks commented Jun 20, 2018

Hi @hosiduy, could you please provide us with a sandbox to reproduce this issue?

@hosiduy
Copy link
Contributor Author

hosiduy commented Jun 21, 2018

Hi @shimks,
I don't know how to create a sandbox for you to create this issue. Please help to show me how to make one.

I did fix this problem by change the code in file: node_modules/dist8/src/repositories/legacy-juggler-bridge.js -> toEntity(model) function.

Before change:

toEntity(model) {
 return new this.entityClass(model.toObject());
}

My change:

toEntity(model) {
  let check;
       if (!model) return null;
       if (typeof model.toObject === 'function') {
            check = new this.entityClass(model.toObject());
        } else {
            check = new this.entityClass(model);
        }
        return check;
}

Hope this help.

@shimks
Copy link
Contributor

shimks commented Jun 21, 2018

I was able to reproduce this issue, thanks. Looks like we'll need to check whether the model we get back in toEntity is an instance of model or not. Would you like to make a PR with the change you've made?

Related to #1378.
cc @raymondfeng @bajtos

@hosiduy
Copy link
Contributor Author

hosiduy commented Jun 22, 2018

Okay @shimks , I will make a PR with the change I've made.

@bajtos bajtos added the LB4 GA label Jul 31, 2018
@bajtos bajtos changed the title Using near filter in where query cause toEntiry(model) bug Using near filter in where query cause toEntity(model) bug Aug 27, 2018
@bajtos bajtos added the p1 label Aug 27, 2018
@virkt25 virkt25 assigned virkt25 and unassigned shimks Sep 7, 2018
@virkt25
Copy link
Contributor

virkt25 commented Sep 13, 2018

@hosiduy Are you still interested / able to work on this issue. I know you forked the repo, once you have made the necessary changes you will have to Open a PR by clicking the Pull Request link within your forked repo.

image

@dhmlau dhmlau added p2 and removed p1 labels Oct 3, 2018
@dhmlau dhmlau assigned hacksparrow and unassigned virkt25 Oct 3, 2018
@dhmlau dhmlau added p1 and removed LB4 GA labels Oct 5, 2018
@susannaHayrapetyan
Copy link

Hi, any news? I'm getting the same issue. Cannot use near filter.

@hacksparrow
Copy link
Contributor

Please refer to #1981 for updates discussions.

@bajtos
Copy link
Member

bajtos commented Jan 8, 2019

Please note that this issue does not require GeoPoint support to be landed. Here, we just need to fix toEntity method in our repository implementation.

@b-admike
Copy link
Contributor

@hosiduy @susannaHayrapetyan We've fixed this recently in juggler. Please try upgrading your dependencies so that you get [email protected] and the issue should be resolved. Feel free to re-open if that's not the case.

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