Relationship data not populate under the related data

Hi Team,

I have created the relationship between the user and the project entity.

I want to populate the projects under the specific user.

user model:

...
'project': [{ type: Mongoose.Schema.Types.ObjectId, ref: 'projects' }],
...

When we are running our app it gives us empty results but multiple projects associated with user.

Thanks,
Sourabh Turkar

Hello @sourabht,

Thanks for reaching out.

To help you out, could you please:

  • Show me if you have any error when starting your server? And in your browser when accessing this related data?
  • Give me the full user and projects models?

Thanks :raised_hands:

Hello @adriguy ,

I have the user model and project model.I need to display list of projects under the users module.

User model defined relationship into forest admin codebase. added below code manually after the lumber update.

module.exports = (mongoose, Mongoose) => {
  // This section contains the properties of your model, mapped to your collection's properties.
  // Learn more here: https://docs.forestadmin.com/documentation/v/v6/reference-guide/models/enrich-your-models#declaring-a-new-field-in-a-model
  const schema = Mongoose.Schema({
    'status': Boolean,
    'first_name': String,
    'project': [{ type: Mongoose.Schema.Types.ObjectId, ref: 'projects' }],
  }, {
    timestamps: false,
  });

  return mongoose.model('users', schema, 'users');
};

in project model user has.


module.exports = (mongoose, Mongoose) => {
  // This section contains the properties of your model, mapped to your collection's properties.
  // Learn more here: https://docs.forestadmin.com/documentation/v/v6/reference-guide/models/enrich-your-models#declaring-a-new-field-in-a-model
  const schema = Mongoose.Schema({
    'user': { type: Mongoose.Schema.Types.ObjectId, ref: 'users' },
    'project_name': String,
    'slug': String,
    'created_at': Date,
    'client_name': String,
    'cover_image_url': String,
    'updated_at': Date,
  }, {
    timestamps: false,
  });

  return mongoose.model('projects', schema, 'projects');
};

I need projects as related data under the user but it is showing the blank result as I mentioned above post screenshot.

Thanks,
Sourabh

@sourabht,

Why do you refer sometimes to gigprojects and sometimes to projects?
The issue might come from here :thinking:

@adriguy ,

I corrected that above post please refer to that. this is only projects.

Thanks,
Sourabh

Hi @sourabht :wave: have you override the /forest/users/:userId/relationships/projects route?

1 Like

No can you please help me on this.

In order to try to reproduce your issue, which is the version of forest-express-mongoose?

forest-express-mongoose version is 7.0.0

If you do a manual find on your model you try to access what is the result?
Have you got a project array?

users.findById('yourId')
.then(user => {
  console.log(user);
});

Can you share with us the related field in your .forestadmin-schema.json
exemple:

{
    "field": "actors",
    "type": ["String"],
    "defaultValue": null,
    "enums": null,
    "integration": null,
    "isFilterable": true,
    "isPrimaryKey": false,
    "isReadOnly": false,
    "isRequired": false,
    "isSortable": true,
    "isVirtual": false,
    "reference": "persons._id",
    "inverseOf": null,
    "validations": []
  }

Hi @Arnaud_Moncel ,

below is the log of user

{
  industry: [],
  project: [],
  _id: 60c9b592c99589172f73fc6f,
  first_name: 'Nedra',
  last_name: 'Sauer',
  created_at: 2021-06-16T08:25:55.093Z,
  updated_at: 2021-06-16T08:25:55.093Z,
  slug: 'nedra-sauer',
  __v: 0
}

project is null but the user is associated with a particular user. In my database.

What should i need to do here to make the association between users and projects?

the issue already debug with @louis on call.

Thanks,
Sourabh

Hello @sourabht :wave:

Thanks for posting the logs it’s clear now :smiley:

As you can see, the user you posted does not have projects associated to him. In the project record associated to your user, the attribute project.user is certainly filed in, but in the user, the user.project is not filed in.

Knowing your are using a NoSQL database, you need to insert every project a user relates to in the user.project array. Once you have added the project ObjectID in the user.project array, then your related data should expose the actual project in the UI.

In your current configuration, only setting project.user to the value of a user will not create the correct link to display the association in your related data.

As an example:

{
  industry: [],
  project: [ObjectId('60c9b592c99589172f73xxxx')],
  _id: 60c9b592c99589172f73fc6f,
  first_name: 'Nedra',
  last_name: 'Sauer',
  created_at: 2021-06-16T08:25:55.093Z,
  updated_at: 2021-06-16T08:25:55.093Z,
  slug: 'nedra-sauer',
  __v: 0
}

This would display the project with Id 60c9b592c99589172f73xxxx in the related data of the user Nedra Sauer.

Can try this with a project ObjectId of your please ?

Looking forward to read you soon,

Steve.

Thank you @Steve_Bunlon .

We will try with project object inside the user.