Sails 1.0 cannot access autogenerated associated entity routes

孤者浪人 提交于 2019-12-25 00:54:33

问题


I have an association between Client and Budget as follows:

//Client.js
module.exports = {
  primaryKey: 'id',

  attributes: {
    id: {
      type: 'number',
      unique: true,
      autoIncrement: true
    },
    name: {
      type: 'string'
    },
    phone: {
      type: 'string',
      unique: true
    },
    email: {
      type: 'string',
      unique: true
    },
    budgets: {
      collection: 'budget',
      via: 'client'
    },
  }
};


// Budget.js
module.exports = {
  primaryKey: 'id',

  attributes: {
    id: {
      type: 'number',
      unique: true,
      autoIncrement: true
    },
    client: {
      model: 'client'
    },
    budgetItems: {
      type: 'json'
    }
  }
};

So, POST is working for both entities so I can create both of them, but:

 GET /budget/1

returns the budget and the id of the associated client.

 GET /budget/1/client

returns the client id without populating(as i've seen in documentation it should be populated).

 GET /client/1

returns the client attributes and there is not a field related with budgets.

 GET /client/1/budgets

returns 404 NOT FOUND

I'm following this and this official documentation

So what I could be missing?

Is just generating one direction associations, and I've compared with official documentation and third party examples and my code looks fine.

Thanks in advance!

Update:

I still looking for troubles and if I run sails with --silly option it shows there is the following route available:

Binding route :: get /client/:parentid/budgets POLICY: localize
Binding route :: get /client/:parentid/budgets POLICY: isauth
Binding route :: get /client/:parentid/budgets BLUEPRINT: populate

but if I try to access returns a 404 Not Found and console shows the following error, thrown by populate.js from Sails core code:

verbo: In populate blueprint action: Specified parent record (1) does not have a budgets.

Update2:

Debugging with sails console I've seen the association is generated properly. Given Client.findOne({id: 1}).populate('budgets').then((client)=>{console.log(client)}) print the client attributes and the associated Budgets but still return 404 Not Found when: GET /client/1/budgets


回答1:


I have created quick demo and it seems working fine for me.

For demo I have used sails version 1.2.2 and sails-disk as database and there is some minor difference in Model attributes as below Client.js

module.exports = {
  attributes: {
    name: {
      type: 'string'
    },
    phone: {
      type: 'string',
      unique: true,
      required: true
    },
    email: {
      type: 'string',
      unique: true,
      required: true
    },
    budgets: {
      collection: 'Budget', // <<== B is capital here
      via: 'client'
    },
  },
};

Budget.js

module.exports = {
  attributes: {
    client: {
      model: 'Client' // <<== C is capital here
    },
    budgetItems: {
      type: 'json'
    }
  },
};

Let me know if this is helpful




回答2:


thanks to SailsJS team we've found the problem and it was related with a third party package and just had to remove it from my project. It was sails-hook-deep-orm who's owner has been warned. I hope someone with the same issue will reach this post.

Thanks u all anyway!!

The issue is available there



来源:https://stackoverflow.com/questions/56165433/sails-1-0-cannot-access-autogenerated-associated-entity-routes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!