如果通过 API 创建,则返回和用户对象不与自定义用户对象关联
Backand user object not associated with custom user object if created through API
我创建了一个自定义 "users" 对象,我知道它与 backand 的用户对象相关联。但是,当我使用 Backand 的 SDK 从我的应用程序创建它们时,我可以看到用户是在 Backand 的注册用户中创建的,而不是在我的自定义对象中 "users"
确保您在 Security & Auth 中有以下安全操作,并且它们的条件设置为 true:
事件 - 在创建过程中,
名称 - 创建我的应用程序用户
代码:
/* globals
$http - Service for AJAX calls
CONSTS - CONSTS.apiUrl for Backands API URL
Config - Global Configuration
*/
'use strict';
function backandCallback(userInput, dbRow, parameters, userProfile) {
// When a new user registers, add her to the users object.
// If you are using a different object for your users then change this action accordingly.
if (parameters.sync)
return {};
if (!parameters)
parameters = {};
parameters.email = userInput.Username;
parameters.firstName = userInput.FirstName;
parameters.lastName = userInput.LastName;
try{
var response = $http({
method: "POST",
url:CONSTS.apiUrl + "/1/objects/users",
params: {parameters: {"sync": true}},
data: parameters,
headers: {"Authorization": userProfile.token}
});
}
catch(err) {
// register user even if there is an error or no users object
}
return {};
}
事件 - 更新期间,
名称 - 更新我的应用程序用户
代码:
/* globals
$http - Service for AJAX calls
CONSTS - CONSTS.apiUrl for Backands API URL
Config - Global Configuration
*/
'use strict';
function backandCallback(userInput, dbRow, parameters, userProfile) {
// When a registered user is changed, change your users object as well.
// If you are using a different object for your users then change this action accordingly.
// Get the user id by the user's email
var currentUser = $http({
method: "GET",
url:CONSTS.apiUrl + "/1/objects/users",
params: {filter:[{"fieldName":"email", "operator":"equals", "value": userInput.Username }]},
headers: {"Authorization": userProfile.token}
});
if (currentUser.data.length == 1) {
var currentUserId = currentUser.data[0].__metadata.id;
var response = $http({
method: "PUT",
url:CONSTS.apiUrl + "/1/objects/users/" + currentUserId + "",
params: {},
data: {"firstName": userInput.FirstName, "lastName": userInput.LastName },
headers: {"Authorization": userProfile.token}
});
}
return {};
}
事件 - 在删除过程中,
名称 - 删除我的应用用户
代码:
/* globals
$http - Service for AJAX calls
CONSTS - CONSTS.apiUrl for Backands API URL
Config - Global Configuration
*/
'use strict';
function backandCallback(userInput, dbRow, parameters, userProfile) {
// When a registered user name is deleted, delete her from your users object as well.
// If you are using a different object for your users then change this action accordingly.
// Get the user id by the user's email
var currentUser = $http({
method: "GET",
url:CONSTS.apiUrl + "/1/objects/users",
params: {filter:[{"fieldName":"email", "operator":"equals", "value": dbRow.Username }]},
headers: {"Authorization": userProfile.token}
});
if (currentUser.data.length == 1) {
var currentUserId = currentUser.data[0].__metadata.id;
var response = $http({
method: "DELETE",
url:CONSTS.apiUrl + "/1/objects/users/" + currentUserId + "",
params: {},
headers: {"Authorization": userProfile.token}
});
}
return {};
}
这似乎是权限问题。也没有很好的记录。如果您的匿名访问设置为(只读)并且您从 "Create My App User" 服务器端 javascript 删除了 try catch 语句,您将在您的客户端中收到 401 错误。将 Anonymous Access 设置为 public 并且它可以第一次尝试。可能有更好的方法来做到这一点,但我仍在研究它我不想给匿名用户那么多的访问权限。但是确实证明是权限问题
我创建了一个自定义 "users" 对象,我知道它与 backand 的用户对象相关联。但是,当我使用 Backand 的 SDK 从我的应用程序创建它们时,我可以看到用户是在 Backand 的注册用户中创建的,而不是在我的自定义对象中 "users"
确保您在 Security & Auth 中有以下安全操作,并且它们的条件设置为 true: 事件 - 在创建过程中, 名称 - 创建我的应用程序用户 代码:
/* globals
$http - Service for AJAX calls
CONSTS - CONSTS.apiUrl for Backands API URL
Config - Global Configuration
*/
'use strict';
function backandCallback(userInput, dbRow, parameters, userProfile) {
// When a new user registers, add her to the users object.
// If you are using a different object for your users then change this action accordingly.
if (parameters.sync)
return {};
if (!parameters)
parameters = {};
parameters.email = userInput.Username;
parameters.firstName = userInput.FirstName;
parameters.lastName = userInput.LastName;
try{
var response = $http({
method: "POST",
url:CONSTS.apiUrl + "/1/objects/users",
params: {parameters: {"sync": true}},
data: parameters,
headers: {"Authorization": userProfile.token}
});
}
catch(err) {
// register user even if there is an error or no users object
}
return {};
}
事件 - 更新期间, 名称 - 更新我的应用程序用户 代码:
/* globals
$http - Service for AJAX calls
CONSTS - CONSTS.apiUrl for Backands API URL
Config - Global Configuration
*/
'use strict';
function backandCallback(userInput, dbRow, parameters, userProfile) {
// When a registered user is changed, change your users object as well.
// If you are using a different object for your users then change this action accordingly.
// Get the user id by the user's email
var currentUser = $http({
method: "GET",
url:CONSTS.apiUrl + "/1/objects/users",
params: {filter:[{"fieldName":"email", "operator":"equals", "value": userInput.Username }]},
headers: {"Authorization": userProfile.token}
});
if (currentUser.data.length == 1) {
var currentUserId = currentUser.data[0].__metadata.id;
var response = $http({
method: "PUT",
url:CONSTS.apiUrl + "/1/objects/users/" + currentUserId + "",
params: {},
data: {"firstName": userInput.FirstName, "lastName": userInput.LastName },
headers: {"Authorization": userProfile.token}
});
}
return {};
}
事件 - 在删除过程中, 名称 - 删除我的应用用户 代码:
/* globals
$http - Service for AJAX calls
CONSTS - CONSTS.apiUrl for Backands API URL
Config - Global Configuration
*/
'use strict';
function backandCallback(userInput, dbRow, parameters, userProfile) {
// When a registered user name is deleted, delete her from your users object as well.
// If you are using a different object for your users then change this action accordingly.
// Get the user id by the user's email
var currentUser = $http({
method: "GET",
url:CONSTS.apiUrl + "/1/objects/users",
params: {filter:[{"fieldName":"email", "operator":"equals", "value": dbRow.Username }]},
headers: {"Authorization": userProfile.token}
});
if (currentUser.data.length == 1) {
var currentUserId = currentUser.data[0].__metadata.id;
var response = $http({
method: "DELETE",
url:CONSTS.apiUrl + "/1/objects/users/" + currentUserId + "",
params: {},
headers: {"Authorization": userProfile.token}
});
}
return {};
}
这似乎是权限问题。也没有很好的记录。如果您的匿名访问设置为(只读)并且您从 "Create My App User" 服务器端 javascript 删除了 try catch 语句,您将在您的客户端中收到 401 错误。将 Anonymous Access 设置为 public 并且它可以第一次尝试。可能有更好的方法来做到这一点,但我仍在研究它我不想给匿名用户那么多的访问权限。但是确实证明是权限问题