问题
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