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

Error when using recursive populate on mongoose document element with empty array #583

Closed
Creiger opened this issue May 18, 2017 · 5 comments

Comments

@Creiger
Copy link
Contributor

Creiger commented May 18, 2017

Steps to reproduce

Create 3 mongoose documents with references ex. channels,users,groups. Channel can have 0:n users, user can have 0:1 groups. Channels schema has users defined like this:
users: [{type: mongooseClient.Schema.Types.ObjectId, ref: 'users', required: true}]
so when you create new Channel document, without any users, mongoose adds by design
users: []
to new document. Then add populate schema to channel hooks
include: { service: 'users', nameAs: 'users', parentField: 'users', childField: '_id', include: [ { service: 'groups', nameAs: 'group', parentField: 'group', childField: '_id' } ] }
and then fetch channels.

You will get an error:

Cannot read property 'toJSON' of null
error: TypeError: Cannot read property 'toJSON' of null
at populateItemArray (/data/www/lukashronec.cz/ptt/node_modules/feathers-hooks-common/lib/services/populate.js:108:12)
at /data/www/lukashronec.cz/ptt/node_modules/feathers-hooks-common/lib/services/populate.js:234:14
at process._tickCallback (internal/process/next_tick.js:103:7)

I also tried non recursive populate with users only and it worked. When i remove those Channels users where they are empty, it works with recursive schema. So only recursive population has problem with empty arrays.

Expected behavior

I would expect that populateItemArray would check if items are an empty array and return again an empty array. Same when i use non recursive populate schema.

Actual behavior

It throws an error.

System configuration

Tell us about the applicable parts of your setup.

"feathers": "^2.1.2",
"feathers-authentication": "^1.2.3",
"feathers-authentication-hooks": "^0.1.3",
"feathers-authentication-jwt": "^0.3.1",
"feathers-authentication-local": "^0.3.4",
"feathers-configuration": "^0.4.1",
"feathers-errors": "^2.8.0",
"feathers-hooks": "^2.0.1",
"feathers-hooks-common": "^3.3.2",
"feathers-mongoose": "^5.1.0",
"feathers-rest": "^1.7.2",
"feathers-socketio": "^2.0.0",

@eddyystop
Copy link
Contributor

You will find your issues might be handled faster is they are posted to the repo they affect, feathers-hooks-common in this case.

Preliminaries for fix are in feathersjs-ecosystem/feathers-hooks-common#190

@eddyystop
Copy link
Contributor

I think you would want to use asArray in this case to ensure you always get an array back, even if it has just 1 element. It looks like that would avoid the problem until a fix lands.

include: {
  service: 'users',
  nameAs: 'users',
  parentField: 'users',
  childField: '_id',
  asArray: true, // workaround
  include: [ ... ]
}

@eddyystop
Copy link
Contributor

Fixed in feathersjs-ecosystem/feathers-hooks-common#191 which will be published as v3.3.3.

Please confirm the fix works for you. Could you afterwards close this issue? Thanks.

@Creiger
Copy link
Contributor Author

Creiger commented May 20, 2017

Thank you eddyystop and sorry for the wrong repo.

@daffl daffl closed this as completed May 29, 2017
@lock
Copy link

lock bot commented Feb 7, 2019

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue with a link to this issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Feb 7, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants