我对以下承诺链有疑问:
Parse.Promise.as().then(function() {
return query.first({useMasterKey: true});
}).then(function(requestData) {
if (!requestData) {
return signUp(fb_ID,fb_accessToken)
} else {
return signIn(fb_ID,fb_accessToken)
}
}).then(function(userdata){
if (userdata.success == "true") {
response.success(userdata)
}
else {
response.error(userdata)
}
})
var signIn = function(fb_ID,fb_accessToken) {
console.log("signin!")
var promise = new Parse.Promise();
var TokenStorage = Parse.Object.extend("tokenStorage");
var query = new Parse.Query(TokenStorage);
query.equalTo('facebookID', fb_ID);
query.ascending('createdAt');
var password;
var username;
var output = {};
var user;
// Check if this ID has previously logged in, using the master key
query.first({ useMasterKey: true }).then(function(tokenData) {
// Update the accessToken if it is different.
if (fb_accessToken !== tokenData.get('fb_accessToken')) {
console.log('1')
user = tokenData.get('user');
user.fetch({
success: function(data) {
username = data.get('username')
tokenData.set('fb_accessToken', fb_accessToken);
/*
password = new Buffer(24);
_.times(24, function(i) {
password.set(i, _.random(0, 255));
password = password.toString('base64')
})
*/
password = (Date.now().toString(36) + Math.random().toString(36).substr(2, 10)).toUpperCase();
user.setPassword(password);
tokenData.set('password',password)
tokenData.set('username',username)
}
}).then(function(){
tokenData.save(null, { useMasterKey: true }).then(function(tokenuser) {
user.save();
return Parse.User.logIn(username, password);
}).then(function(data) {
// Return the user object.
output.success = true
output.isnewuser = false
output.username = username;
output.password = password;
output.fb_accessToken = fb_accessToken;
//return Parse.Promise.as(output);
promise.resolve(output)
//return promise
});
})
} else {
console.log('2')
Parse.Promise.as().then(function() {
username = tokenData.get('username');
password = tokenData.get('password');
}).then(function(){
Parse.User.logIn(username, password)
.then(function(data) {
// Return the user object
output.success = true
output.isnewuser = false
output.username = username;
output.password = password;
output.fb_accessToken = fb_accessToken;
console.log('oo'+JSON.stringify(output))
//return Parse.Promise.as(output);
promise.resolve(output)
});
})
}
})
return promise
}
var signUp = function(fb_ID,fb_accessToken) {
console.log("signup")
ExtendFacebookAccessToken(fb_accessToken).then(function(new_fb_accessToken){
var TokenStorage = Parse.Object.extend("tokenStorage");
var restrictedAcl = new Parse.ACL();
restrictedAcl.setPublicReadAccess(false);
restrictedAcl.setPublicWriteAccess(false);
var user = new Parse.User();
// Generate a random username and password.
/*var username = new Buffer(24);
var password = new Buffer(24);
_.times(24, function(i) {
username.set(i, _.random(0, 255));
password.set(i, _.random(0, 255));
});
*/
var username = (Date.now().toString(36) + Math.random().toString(36).substr(2, 10)).toUpperCase();
var password = (Date.now().toString(36) + Math.random().toString(36).substr(2, 10)).toUpperCase();
user.set("username", username.toString('base64'));
user.set("password", password.toString('base64'));
// Sign up the new User
return user.signUp().then(function(user) {
console.log('user'+JSON.stringify(user))
// create a new TokenStorage object to store the user+GitHub association.
var ts = new TokenStorage();
ts.set('facebookID', fb_ID);
ts.set('fb_accessToken', new_fb_accessToken);
ts.set('username',username)
ts.set('password',password)
ts.set('user', user);
ts.setACL(restrictedAcl);
// Use the master key because TokenStorage objects should be protected.
return ts.save(null, { useMasterKey: true });
}).then(function(tokenStorage) {
var output
var promise = new Parse.Promise();
output.username = username;
output.password = password;
output.process = "signup";
output.fb_accessToken = new_fb_accessToken;
output.success = true;
output.isnewuser = true
promise.resolve(output);
return promise
});
})
}
SignUp和SignIn都返回一个promise,但问题是最后一个('then(function(userdata)...')在SignUp和SignIn返回'userdata'之前执行。我认为前面的返回SignUp或SignIn函数足以强制最后一个承诺等待userdata,但事实并非如此。任何想法?
答案 0 :(得分:0)
您的注册方法未返回承诺。相反,您最有可能获得立即解决的undefined
变化:
var signUp = function(fb_ID,fb_accessToken) {
console.log("signup")
ExtendFacebookAccessToken(fb_accessToken).then(function(new_fb_accessToken)
要:
var signUp = function(fb_ID,fb_accessToken) {
console.log("signup")
return ExtendFacebookAccessToken(fb_accessToken).then(function(new_fb_accessToken)
请拜托,缩进你的代码:(