简体   繁体   中英

SailsJS association

I am trying to get a association with two models:

User -> Module

My User model:

module.exports = {
  tableName: 'user',
  autoCreatedAt: false,
  autoUpdatedAt: false,
  attributes: {
    username: {
      type: 'string',
      required: true,
      size: 45
    },
    password: {
      type: 'string',
      required: true,
      size: 45
    },
    firstname: {
      type: 'string',
      required: true,
      size: 45
    },
    lastname: {
      type: 'string',
      required: true,
      size: 45
    },
    grades: {
      type: 'json',
      defaultsTo:[]
    },
    ....

Module model:

module.exports = {
  tableName: 'module',
  autoCreatedAt: false,
  autoUpdatedAt: false,
  attributes: {

    name: {
      type: 'string',
      required:true
    },

    spec_id: {
      type: 'integer',
      required: true,
      enum: [1, 2, 3, 4]
    },

  }
};

The user collection looks like:

{
    "_id": {
        "$oid": "58763b49458a93f4785fff0e"
    },
    "grades": [
        {
            "grade": 7.5,
            "mod_id": "587686a4f36d284ed588d72c",
            "try": 1
        },
        {
            "grade": 6.4,
            "mod_id": "587686a4f36d284ed588d72c",
            "try": 2
        }
    ],
    "role_id": "58766274734d1d50c512f493",
    "class_id": "58768634f36d284ed588d6e6",
    "spec_id": "587686a4f36d284ed588d72c",
    "username": "-",
    "password": "-",
    "firstname": "-",
    "lastname": "-",
    "email": "test@test.nl",
    "birthdate": "1995-05-1995",
    "_csrf": "null"
}

The query I used to log the above result:

var findOneUser = User.find();
findOneUser.where({id: req.session.userID});
findOneUser.populateAll()
findOneUser.exec(function callBack(err,user){
      var singleUser = user.pop().toJSON();
      console.log(singleUser);
 });

How could I achieve the following in my models (using user.grades.mod_id as join key), so having a module object inside each of my grades objects (I am using MongoDB)

    {
  "_id": {
    "$oid": "58763b49458a93f4785fff0e"
  },
  "grades": [
    {
      "grade": 7.5,
      "module": {
        "name": "bla",
        "description": "-",
        "spec_id": 1
      },
      "try": 1
    },
    {
      "grade": 6.4,
      "module": {
        "name": "bla",
        "description": "-",
        "spec_id": 1
      },
      "try": 2
    }
  ],
  "role_id": "58766274734d1d50c512f493",
  "class_id": "58768634f36d284ed588d6e6",
  "spec_id": "587686a4f36d284ed588d72c",
  "username": "-",
  "password": "-",
  "firstname": "-",
  "lastname": "-",
  "email": "test@test.nl",
  "birthdate": "-",
  "_csrf": "null"
}

I saw alot on nested associations; but I think this is a bit different, also found this.. sails.js nested models It's kind of similar, but comparing to this example I'd want to join with a value inside of the 'profile' JSON object.

There are 2 ways to solve this:

Create a separate Grade model. It will be stored separately in MongoDB (not using Mongo in preferred way)

OR Write code to populate modules after getting user

User.findOne(1).exec(function(err, user) {
  user = user.toJSON();
  var moduleIds = _.map(user.grades, 'mod_id');

  Module.find(moduleIds).exec(function(err, modules) {
    _.forEach(user.grades, function(grade) {
      var module = _.find(modules, { 'id': grade.mod_id });
      grade.module = module;
    });
    console.log('User', user);
    return;
  });
});

Not easy to understand where you have issue as it is straight forward after looking into docs.

in User.js model:

mod_id: {
model: 'module' // or modules depends how you have it declared
}

in Module.js model:

users: {
collection: 'user',
via: 'mod+id'
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM