Sails nested model collection

爱⌒轻易说出口 提交于 2019-12-11 01:09:20

问题


i have 3 models.

User model:

module.exports =
  schema: true
  attributes:
    login:
      type: 'string'
      required: true
    hosts:
      collection: 'host'
      via: 'owners'
    accounts:
      collection: 'account'
      via: 'users'

Host model:

module.exports =

  attributes:
    url:
      type: 'string'
      required: true
      unique: true
    owners:
      collection: 'user'
      via: 'hosts'
    accounts:
      collection: 'account'
      via: 'host'

Account model:

module.exports =

  attributes:
    provider:
      type: 'string'
      required: true
    password:
      type: 'string'
    users:
      collection: 'user'
      via: 'accounts'
    host:
      model: 'host'

And i whant found all Users hosts with accounts. How do it?

This option works but I'm not sure that he's good, I'll be very grateful if you tell how to do differently. Or maybe change something in my version.

User
.findOne(id)
.populate('hosts')
.then (hosts) ->
  hostsObj = []
  for host in hosts.hosts
    accounts = Host
    .find(host.id)
    .populate('accounts')
    .then (account) ->
      hostsObj.push(account[0])
      return account
  return [hostsObj,accounts]
.spread (hostsObj, accounts) ->
  done(null, hostsObj)
.catch (err) ->
  return done('Not hosts') if err

来源:https://stackoverflow.com/questions/27342375/sails-nested-model-collection

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