2015-06-19 60 views
0

民間,創建用戶驗證錯誤的方法與aldeed集合

我需要幫助。我已經有一段時間了,現在似乎無法解決這個問題。它應該很簡單。我正在使用aldeed collection2,我似乎無法通過創建用戶帳戶方法拋出驗證錯誤。我的架構是非常標準的,附着於meteor.users集合:

Schema={} 
//SimpleSchema.debug = true; 


Schema.UserProfile = new SimpleSchema({ 

    picture: { 
    type: String, 
    optional: true 
    }, 
    updatedAt: { 
    type: Date, 
    autoValue: function() { 
     if (this.isUpdate) { 
     return new Date(); 
     } 
    }, 
    denyInsert: true, 
    optional: true 
    }, 
    roles: { 
    type: String, 
    optional: true 
    } 
}); 

Schema.User = new SimpleSchema({ 
    _id: { 
    type: String, 
    regEx: SimpleSchema.RegEx.Id, 
    optional: true, 
    denyUpdate: true 
    }, 

    emails: { 
    type: [Object], 
    optional: true 
    }, 
    "emails.$.address": { 
    type: String, 
    regEx: SimpleSchema.RegEx.Email, 
    label: "" 

    }, 
    "emails.$.verified": { 
    type: Boolean, 
    optional: true 
    }, 
    createdAt: { 
    optional: true, 
    type: Date, 
    autoValue: function() { 
     if (this.isInsert) { 
     return new Date; 
     } else if (this.isUpsert) { 
     return {$setOnInsert: new Date}; 
     } else { 
     this.unset(); 
     } 
    } 
    }, 
    profile: { 
    type: Schema.UserProfile, 
    optional: true 
    }, 
    services: { 
    type: Object, 
    optional: true, 
    blackbox: true 
    }, 

    // Option 2: [String] type 
    // If you are sure you will never need to use role groups, then 
    // you can specify [String] as the type 
    roles: { 
    type: [String], 
    optional: true, 
    autoValue: function() { 
     if (this.isInsert) { 
     return ['user']; 
     } else if (this.isUpsert) { 
     return {$setOnInsert: ['user']}; 
     } else { 
     this.unset(); 
     } 
    } 
    }, 

    password: { 
    type: String, 
    label: "Password", 
    min: 6 
    } 

}); 

/* Attach schema to Meteor.users collection */ 
Meteor.users.attachSchema(Schema.User); 

用於創建用戶我的服務器上的方法是象下面這樣:

Accounts.config({ 
    forbidClientAccountCreation : true 
}); 

//creates user on server 
Meteor.methods({ 
    createNewUserAccount: function(userdata) { 
    var userId; 
    check(userdata, Schema.User); 
    //console.log(userdata); 

    userId = Accounts.createUser({ 
     email: userdata.emails[0].address, 
     password: userdata.password, 
     profile: userdata.profile 
    }); 
    //console.log(userId); 
    return userId; 
    } 
}); 

Accounts.onCreateUser(function(options, userdata) { 
    //user.profile = {}; 
    // we wait for Meteor to create the user before sending an email 
    //need to address the exception when existing email is tried for signing up 
    Meteor.setTimeout(function() { 
    Accounts.sendVerificationEmail(userdata._id); 
    }, 2 * 1000); 
return userdata; 
}); 

爲我的客戶,我有以下的註冊。 js

Template.signup.events({ 
    'submit form': function(e){ 
    // Prevent form from submitting. 
    e.preventDefault(); 
    //console.log(doc); 
    user = { 
     'email.$.address': $('[name="emails.0.address"]').val(), 
     password: $('[name="password"]').val() 
    }; 

    Meteor.call('createNewUserAccount', user, function(error) { 
     if (error) { 
     return alert(error.reason); 
     } else { 
     Router.go('/'); 
     } 
    }); 

    } 

有誰知道我在做什麼錯?該架構不驗證電子郵件address.I得到錯誤: email.0.address不是由規劃允許的

回答

1

你創建一個對象:

user = { 
    'email.$.address': $('[name="emails.0.address"]').val(), 
    password: $('[name="password"]').val() 
}; 

的關鍵是文字字符串'email.$.address'

所以,當你這樣做:

userId = Accounts.createUser({ 
     email: userdata.emails[0].address, 
     password: userdata.password, 
     profile: userdata.profile 
    }); 

email密鑰找不到userdata.emails[0],因爲沒有emails密鑰。相反,關鍵是'email.$.address'。另外,架構沒有名爲email.$.address的密鑰。它有一個所謂的emails.$.address

嘗試:

Template.signup.events({ 
    'submit form': function(e){ 
    // Prevent form from submitting. 
    e.preventDefault(); 
    //console.log(doc); 
    user = { 
     'emails.$.address': $('[name="emails.0.address"]').val(), 
     password: $('[name="password"]').val() 
    }; 

    Meteor.call('createNewUserAccount', user, function(error) { 
     if (error) { 
     return alert(error.reason); 
     } else { 
     Router.go('/'); 
     } 
    }); 

    } 

然後

//creates user on server 
Meteor.methods({ 
    createNewUserAccount: function(userdata) { 
    var userId; 
    check(userdata, Schema.User); 
    //console.log(userdata); 

    userId = Accounts.createUser({ 
     email: userdata['emails.$.address'], 
     password: userdata.password, 
     profile: userdata.profile 
    }); 
    //console.log(userId); 
    return userId; 
    } 
}); 
+0

THKS很多。我在電子郵件:userdata。['emails。$。address'],上得到了一個編譯錯誤。意外的標記。 – flt123

+0

哦,對不起,請刪除userdata和[ – fuzzybabybunny

+0

很多。它現在有效 – flt123