2014-09-03 83 views
5

我試圖測試自定義登錄方法的功能,所以這是我的客戶端獲取服務器錯誤:當使用自定義登錄方法

Meteor.loginWithCode = function(phone, code) { 

    Accounts.callLoginMethod({ 
    methodArguments: [{ 
     hascode: true, 
     phone: phone, 
     code: code 
    }], 
    userCallback: function loginCallback (error, result) { 
     console.log(error, result); 
    } 
    }); 
}; 

這是服務器:

Accounts.registerLoginHandler('login', function(loginRequest) { 

    var user = Meteor.users.findOne({phone: loginRequest.phone}); 

    if(user.code !== loginRequest.code) { 
    return null; 
    } 

    var stampedToken = Accounts._generateStampedLoginToken(); 
    var hashStampedToken = Accounts._hashStampedToken(stampedToken); 

    Meteor.users.update(user._id, 
    {$push: {'services.resume.loginTokens': hashStampedToken}} 
); 

    return { 
    id: user._id, 
    token: stampedToken.token 
    }; 
}); 

爲什麼我我越來越

Exception while invoking method 'login' Error: A login method must specify a userId or an error 

當我做Meteor.loginWithCode('123456789', '123');

回答

8

您應該返回userIdid

Accounts.registerLoginHandler('login', function(loginRequest) { 
    ... 
    ... 
    return { 
    userId: user._id, 
    token: stampedToken.token 
    }; 
}) 

如果登錄不成功,再通過error代替userId

證明:

if (!result.userId && !result.error) 
    throw new Error("A login method must specify a userId or an error"); 

Line 255-256