我正在尝试使用 Restangular 从基于 Node.js 的 MongoDB 实例中获取一系列对象;但是,我无法保证我希望通过 ID 抓取的对象数量始终相同。
为了演示,这里是原理的代码片段:
Restangular
.several('users',
userList[0], userList[1], userList[2], userList[3], userList[4],
userList[5], userList[6], userList[7], userList[8], userList[9])
.get().then(function (users) { //...
userList
是作为方法的一部分传入的 ID 数组:
requestUsersById = function (userList) { //...
问题是我不能保证数组的大小。有没有办法使用 Restangular 传递一组 ID?或者,我只是坚持为每个单独的请求?
理想的结果应该是这样的:
Restangular
.several('users', userList)
.get().then(function (users) { //...
最佳答案
Restangular API 似乎本身并不支持这一点,但我相信您可以通过使用 apply()
method 来完成您想要做的事情。 .
在这种情况下,您会将集合 users
的名称附加到 userList
数组的头部。试试这个:
// Store the userList in a new array to preserve the initial list
// (not sure if you use it somewhere else
var usersQuery = userList;
// Shift the collection name to be the first parameter in the array
usersQuery.unshift("users");
// Perform the Restangular call
Restangular
.several.apply(null,usersQuery)
.get().then(function (users) { //...
关于javascript - ID 数组、Restangular、.several(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35637629/