0

Sails.js application fails to create one-to-many relationship in mongo db:

According to official Documentation I created two linked one-to-many entities, Post-Likes. I located them in api/models folder and defined as follows:

Like.js

module.exports = {
  attributes: {
    post: {
      model: 'post',
      required: true
    }
}

Post.js

module.exports = {
  attributes: {
    likes: {
      collection: 'like',
      via: 'post'
    },
    user: {
      model: 'user'
    }
  }
}

And I populate likes for the post with a following statement on the page:

  const posts = await Post.find({user: id})
    .sort('createdAt DESC')
    .populate('user')
    .populate('likes');

After I run sails.js app with command sails lift --drop and load the page with code above I receive the following error message in console:

error: Sending 500 ("Server Error") response: UsageError: Invalid populate(s). Details: Could not populate like. There is no attribute named like defined in this model.

at Object.module.exports [as user/profile] (/Users/new/Developer/ios/social/web_social/api/controllers/user/profile.js:8:30)
at processTicksAndRejections (internal/process/task_queues.js:93:5)

I checked mongo db instance and I cannot see any empty or null like attribute for post entity:

enter image description here

Ievgen
  • 1,999
  • 2
  • 12
  • 24

1 Answers1

0

try to make your model name initCap. like below

    module.exports = {
  attributes: {
    post: {
      model: 'Post',
      required: true
    }
}
Abhi Patel
  • 214
  • 1
  • 6