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

Handle null on findOne function in DefaultCrudRepository class #1378

Closed
3 tasks
hosiduy opened this issue May 30, 2018 · 5 comments
Closed
3 tasks

Handle null on findOne function in DefaultCrudRepository class #1378

hosiduy opened this issue May 30, 2018 · 5 comments

Comments

@hosiduy
Copy link
Contributor

hosiduy commented May 30, 2018

Description / Steps to reproduce / Feature proposal

Hi there,
In DefaultCrudRepository class in /node_modules/@loopback/repository/dist8/src/repositories/legacy-juggler-bridge.js file, we have code:

async findOne(filter, options) {
        const model = await ensurePromise(this.modelClass.findOne(filter, options));
        return this.toEntity(model);
    }
toEntity(model) {
        return new this.entityClass(model.toObject());
    }

Current Behavior

The bug happends when function: findOne return null and pass null to function: toEntiry(). So we have null.toObject() -> Cannot read property 'toObject' of null error.

Expected Behavior

Expect: same as findById function.

async findOne(filter, options) {
        const model = await ensurePromise(this.modelClass.findOne(filter, options));
        if (!model) {
            throw new Error(`no ${this.modelClass.name} found with id "${id}"`);
        }
        return this.toEntity(model);
    }
    async findById(id, filter, options) {
        const model = await ensurePromise(this.modelClass.findById(id, filter, options));
        if (!model) {
            throw new Error(`no ${this.modelClass.name} found with id "${id}"`);
        }
        return this.toEntity(model);
    }

Thanks.

Acceptance Criteria (from @jannyHou)

See Reporting Issues for more tips on writing good issues

@jannyHou
Copy link
Contributor

jannyHou commented May 30, 2018

@hosiduy Thank you for catching and bringing up the issue.
As a more general solution, I think we should check null or empty array for all CRUD methods in file: https://github.com/strongloop/loopback-next/blob/c553f1171ca86eb13ba7473d73d2e0d1b8de29de/packages/repository/src/repositories/legacy-juggler-bridge.ts

For method like findOne, we probably don't want to throw error for empty result, we can handle null in toEntity by returning an empty entity.

@dhmlau
Copy link
Member

dhmlau commented Jun 8, 2018

@jannyHou @bajtos , do we have enough information to estimate or should put to our backlog directly? Thanks.

@jannyHou
Copy link
Contributor

Acceptance Criteria:

@jannyHou
Copy link
Contributor

@dhmlau I would suggest we do a quick estimation tomorrow. Then put to our backlog.

@bajtos
Copy link
Member

bajtos commented Jun 11, 2018

Either return an empty entity or throw error(story owner can decide which one is proper)

IMO, findOne should return null when no entity was found for consistency with the current LB3.x behavior. I am proposing the following fix:

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

@jannyHou jannyHou changed the title Error on findOne function in DefaultCrudRepository class Handle null or error on ERUD function in DefaultCrudRepository class Jun 12, 2018
@jannyHou jannyHou changed the title Handle null or error on ERUD function in DefaultCrudRepository class Handle null on findOne function in DefaultCrudRepository class Jun 12, 2018
@bajtos bajtos added the p1 label Aug 13, 2018
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

4 participants