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

.populate() returns findOne() on empty string. This results in a random find. #1325

Closed
PVermeer opened this issue Jul 21, 2019 · 2 comments
Closed
Labels

Comments

@PVermeer
Copy link

PVermeer commented Jul 21, 2019

First thanks for this great repo!

Case

Bug

Issue

Populate on a ref field that has an empty string returns a random document from that collection.

Possible solution

Looking at the source, the populate() is the return of a findOne(). findOne() will return the first value when no arguments are supplied. I would suggest to check for falsy on the value of the path argument of the populate function and to return null if false.

Info

  • Environment: Angular 8
  • Adapter: IndexedDB
  • Stack:

Code

Example:

transaction.populate('account').then(x => console.log(x.name));

// Schema to populate
{
title: 'sdfsdgsg',
  version: 0,
  description: 'cvbcvbvb',
  type: 'object',

  properties: {
    account: {
      ref: 'accounts',
      type: 'string',
    },
 }

// Schema account
{
title: 'fghfgh',
  version: 0,
  description: 'tryrtytry',
  type: 'object',

  properties: {
    name: {
      type: 'string'      
    }    
  }
}
 

Suggested fix:

populate(path) {


        const schemaObj = this.collection.schema.getSchemaByObjectPath(path);
        const value = this.get(path);

// fix or something like this
if (!value) { return Promise.resolve(null); }
//---      

        if (!schemaObj) {
            throw newRxError('DOC5', {
                path
            });
        }
        if (!schemaObj.ref) {
            throw newRxError('DOC6', {
                path,
                schemaObj
            });
        }

        const refCollection = this.collection.database.collections[schemaObj.ref];
        if (!refCollection) {
            throw newRxError('DOC7', {
                ref: schemaObj.ref,
                path,
                schemaObj
            });
        }

        if (schemaObj.type === 'array')
            return Promise.all(value.map(id => refCollection.findOne(id).exec()));
        else
            return refCollection.findOne(value).exec();
    },




@pubkey
Copy link
Owner

pubkey commented Jul 22, 2019

@PVermeer thanks for investigation.
The proposed fix looks good for me. Do you have the time to create a PR with unit-test and the fix?

@PVermeer
Copy link
Author

I'll try to look into it this week.

This was referenced Jul 27, 2019
@pubkey pubkey closed this as completed in a0cbaab Aug 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants