2015-10-06 42 views
1

如何得到这个模型的数据:如何获得相关模型将这些数据与Sails.js + Angular.js和RestAngular

//User.js 
module.exports={ 
name:{ 
type:'string' 
}, 
pets:{ 
collection:'pet', 
via:'owner' 
}, 
cars:{ 
collection:'car', 
via:'owner' 
} 
} 

//Pet.js 
module.exports={ 
name:{ 
type:'string' 
}, 
owner:{ 
model:'user' 
}, 
doctors:{ 
collection:'doctor', 
via:'pets' 
} 
}; 

//Car.js 
module.exports={ 
color:{ 
type:'string' 
}, 
owner:{ 
model:'user' 
} 
} 
//doctor.js 
module.exports={ 
name:{ 
type:'string' 
}, 
pets:{ 
collection:'pet', 
via:'doctors' 
} 
    }; 

使用RESTAPI路线与Restangular消耗,如何让所有的宠物车主拥有蓝色的车?如何使用多个关系(宠物< - 所有者 - >汽车)

使用RestApi路线消耗与Restangular,如何让所有用户与宠物有关系与先生XYZ医生?如何使用多个关系用户 - >宠物< - >医生)

感谢您的关注,如果我的问题需要改进,请评论,我会改正任何问题。

回答

2

Deep Queries目前还不支持,有一天。

为什么不一样的东西:

/// Find all blue cars and get their owners 
Cars.find({color:'blue'}).populate('owners').exec(function(err, bluecars) { 

    // unique list of owners 
    var owners = _.uniq(bluecars, function(el) { 
    return el.owner.id; 
    }); 

    // get all complete owners, with pets + cars 
    async.map(
    owners, 
    function(cb) { 
     Owners.findOne({id: el.id}) 
     .populate('pets') 
     .populate('cars') 
     .exec(function(err, data) { 
     cb(err, data); 
     } 
    }, 
    function(err, results) { 
     // this is now a collection of all owners that have blue cars 
     // from this you can see all their pets 
     console.log(results); 
    }); 
}); 

是您做了很多电话到数据库中,但它是非常简单和清晰。

一旦性能成为问题,并希望业主,汽车&宠物都在同一个数据库上,你可以使用连接编写自己的查询。

+1

令人难以置信的代码人,谢谢!和“async.map(拥有者...”是逗号缺失? – Slinker009

+0

哎呀!修正:) –

相关问题