我正在尝试使用Parse Cloud为String值实现一个简单的getter和setter函数,但由于某种原因它不起作用。我搜索了Parse文档和论坛,但找不到原因。请告诉我。
function setPassword(newPass){
console.log('setPassword() started with input ' +newPass);
var PasswordItem = Parse.Object.extend("PasswordItem");
var query = new Parse.Query(PasswordItem);
if (query.length > 0) {
if (query[0].password != newPass) {
console.log('setPassword() found existing password. replacing it with ' +newPass);
query[0].password = newPass;
}else{
console.log('setPassword() found existing password. existing password is the same as the new one');
}
}else{
console.log('setPassword() no existing password. creating new password: ' +newPass);
var passwordItem = new PasswordItem();
passwordItem.set("password", newPass);
passwordItem.save(null, {
success: function (results) {
console.log("Save ok");
response.success(results);
},
error: function (results, error) {
console.log("Save error");
response.error(error);
}});
}
}
function getPassword(){
console.log('getPassword() started');
var password = new String();
var PasswordItem = Parse.Object.extend("PasswordItem");
var query = new Parse.Query(PasswordItem);
if (query.length > 0) {
password = query[0].password;
}else{
password = "";
}
console.log('getPassword() returns ' +password);
return password;
}
请注意,请忽略与密码相关的安全问题,这只是一个例子。
答案 0 :(得分:0)
经过多次尝试,我得到了这段代码。似乎我理解的主要问题是Parse函数save()是异步的,因此我不能期望在不使用Futures的情况下获得结果。 我是JavaScript的新手,所以我将不胜感激。
function getPassword(req, res){
console.log('getPassword() started');
var query = new Parse.Query(PasswordItem);
query.descending('createdAt');
query.find({
success: function(results) {
console.log('getPassword() successfully found ' +results.length +' passwords');
if (results.length > 0){
password = results[0].get("password");
console.log('getPassword() got password ' +password);
}else{
password = "";
console.log('getPassword() got empty password');
}
},
error: function(error) {
console.log('getPassword() error searching for passwords ' +error.message);
password = null;
}
}).then(function() {
renderAll(req, res);
});
console.log('getPassword() ended');
}
function setPassword(req, res){
console.log('setPassword() started with input ' +newPass);
Parse.Cloud.useMasterKey();
var newPass = req.body.password;
var query = new Parse.Query(PasswordItem);
if (query.length > 0) {
if (query[0].password != newPass) {
console.log('setPassword() found existing password. replacing it with ' +newPass);
query[0].password = newPass;
}else{
console.log('setPassword() found existing password. existing password is the same as the new one');
}
}else{
console.log('setPassword() no existing password. creating new password: ' +newPass);
var passwordItem = new PasswordItem();
passwordItem.set("password", newPass);
passwordItem.save(null, {
success: function(results) {
console.log("Save ok");
},
error: function(results, error) {
console.log("Save error");
}
}).then(function(input) {
getPassword(req, res);
});
}
}
function clearPasswords(req, res){
console.log('clearPasswords() started');
Parse.Cloud.useMasterKey();
var query = new Parse.Query(PasswordItem);
query.descending('createdAt');
query.find({
success: function(results) {
console.log('clearPasswords() successfully found ' +results.length +' passwords');
for (var i = 0; i < results.length; i++) {
results[i].destroy();
}
},
error: function(error) {
console.log('clearPasswords() error searching for passwords ' +error.message);
}
}).then(function() {
setPassword(req, res);
});
console.log('clearPasswords() ended');
}