Javascript AngularJS+;按预期解析不受用户ID约束的API调用服务
对于我的应用程序,我为Address创建了一个服务,它允许我为任何给定的用户操作Address对象。除了我的标准CRUD函数外,我还需要一个函数来列出指定Parse.User的任何地址 services.jsJavascript AngularJS+;按预期解析不受用户ID约束的API调用服务,javascript,angularjs,rest,parse-platform,Javascript,Angularjs,Rest,Parse Platform,对于我的应用程序,我为Address创建了一个服务,它允许我为任何给定的用户操作Address对象。除了我的标准CRUD函数外,我还需要一个函数来列出指定Parse.User的任何地址 services.js .factory('Address',['$http', 'PARSE_CREDENTIALS', function ($http,PARSE_CREDENTIALS) { return { // constrain to User ID getAl
.factory('Address',['$http', 'PARSE_CREDENTIALS', function ($http,PARSE_CREDENTIALS) {
return {
// constrain to User ID
getAll: function(userId) {
return $http.get('https://api.parse.com/1/classes/Address', {
headers: {
'X-Parse-Application-Id': PARSE_CREDENTIALS.APP_ID,
'X-Parse-REST-API-Key': PARSE_CREDENTIALS.REST_API_KEY,
'Content-Type' : 'application/json'
},
params: { "userId": userId }
});
},
// ...get(), edit(), add(), delete()
.controller('AddrCtrl', ['$scope', '$state', '$stateParams', '$rootScope', 'Address',
function($scope, $state, $stateParams, $rootScope, Address) {
Address.getAll($rootScope.user.id)
.success( function(data) {
console.log(data);
$scope.addresses = data.results;
})
}]);
controllers.js
.factory('Address',['$http', 'PARSE_CREDENTIALS', function ($http,PARSE_CREDENTIALS) {
return {
// constrain to User ID
getAll: function(userId) {
return $http.get('https://api.parse.com/1/classes/Address', {
headers: {
'X-Parse-Application-Id': PARSE_CREDENTIALS.APP_ID,
'X-Parse-REST-API-Key': PARSE_CREDENTIALS.REST_API_KEY,
'Content-Type' : 'application/json'
},
params: { "userId": userId }
});
},
// ...get(), edit(), add(), delete()
.controller('AddrCtrl', ['$scope', '$state', '$stateParams', '$rootScope', 'Address',
function($scope, $state, $stateParams, $rootScope, Address) {
Address.getAll($rootScope.user.id)
.success( function(data) {
console.log(data);
$scope.addresses = data.results;
})
}]);
在我的角度模板中,视图确实返回地址对象。但是当它只返回带有相应用户ID的地址对象时,它会返回所有地址对象。为了澄清,Address类有一个userId指针列。每个地址只有一个用户
以下是AddrCtrl
在控制台中返回的日志消息:
Object {results: Array[2]}
results: Array[2]
0: Object
firstName: "(test)"
lastName: "test"
// more unrelated properties
objectId: "yUEuFjLlzs"
updatedAt: "2014-12-02T20:17:55.608Z"
userId: Object
__type: "Pointer"
className: "_User"
objectId: "q1KADkp4i1"
我假设这个问题存在于我的$http.get()函数中。最后,我的问题是:为什么我的params
选项不将我的data.results
约束为仅与一个Parse.User关联的地址对象
我不想要的答案:
返回所有地址对象,仅将与Parse.User.current()id匹配的对象保存到$scope中
您需要使用
where
子句来执行查询
如果userId
的数据类型是Pointer
,则应编写如下内容:
{"where": JSON.stringify({
"userId": {"__type":"Pointer","className":"_User","objectId":"userId"}}
)}