2015-07-10 96 views
1

我有一個客戶端功能,用於註冊一個用戶傳遞數據對象到服務器來創建一個用戶。我在客戶端完全看到數據對象,但是當服務器功能運行時(registerNewUser),它會失敗並顯示數據未定義。流星客戶端功能不傳遞數據到服務器功能Accounts.createUser

在客戶端,register.js

if (Meteor.isClient){ 

    Template.registrationStep2.events({ 

     'submit form': function(e, template) { 
      e.preventDefault(); 

      if (Session.get("registrationInstitutionID") && Session.get("registrationInstitutionRole")) { 

       var data = []; 

       var institutionID = template.find('#institutionID').value; 
       var institutionRole = template.find('#institutionRole').value; 
       var institutionName = template.find('#institutionName').value; 

       data.login = template.find('#login').value; 
       console.log("login: " + data.login); 

       var firstName = template.find('#firstName').value; 
       console.log("firstName: " + firstName); 
       var lastName = template.find('#lastName').value; 
       console.log("lastName: " + lastName); 

       data.email = template.find('#email').value; 
       console.log("email: " + data.email); 
       data.password = template.find('#password').value; 

       data.profile = { 
        firstName: firstName, 
        lastName: lastName, 
        role: institutionRole, 
        institutionID: institutionID 
       } 

       Meteor.call('registerNewUser', data, function(error, result){ 
        Meteor.log.trace("User Data Object", data); 
        console.log(data); 
        if(error){ 
         console.log("error", error); 
         Meteor.log.error("Error Creating User", error); 
        } 
        if(result){ 
         console.log("SUCCESS adding user."); 
         Meteor.log.error("Success Result", result); 

         Session.set("flashType", "success"); 
         Session.set("flashMessage", "Your account has been created."); 

         Router.go('registrationComplete'); 
        } 
       }); 

      } else { 
       // alert("Need institution"); 
       $('#needInstitutionModal').modal('show'); 
      } 
     } 
    }); 
} 

這裏是server.js

Meteor.methods({ 
    'registerNewUser': function(data) { 
     Meteor.log.debug("User Data Object on Server", data); 
     console.log(data); 
     return Accounts.createUser({ 
      username: data.login, 
      email: data.email, 
      password: data.password, 
      profile: data.profile 
    } 
}); 

如果我硬編碼值對的createUser功能一切運作正常。但是當我使用數據對象時,每個字段都會出現這樣的錯誤。 (我從控制檯日誌中知道在調用服務器功能之前客戶端中存在的數據對象)。

下面是具體的錯誤行的情況下,很難見下文:Exception while invoking method 'registerNewUser' Error: Match error: Expected string, got undefined in field username

下面是從日誌輸出相關:

I20150710-07:56:51.072(-5)? (07:56:51) [DEBUG @ programs/server/packages/ostrio_loggerconsole.js:67] - Object: 
I20150710-07:56:51.072(-5)? { 
I20150710-07:56:51.072(-5)?  "time": "2015-07-10T12:56:51.070Z", 
I20150710-07:56:51.072(-5)?  "level": "DEBUG", 
I20150710-07:56:51.072(-5)?  "message": "User Data Object on Server", 
I20150710-07:56:51.072(-5)?  "additional": [] 
I20150710-07:56:51.072(-5)? } 
I20150710-07:56:51.075(-5)? logging data object on server ================+ 
I20150710-07:56:51.075(-5)? [] 
I20150710-07:56:51.077(-5)? Exception while invoking method 'registerNewUser' Error: Match error: Expected string, got undefined in field username 
I20150710-07:56:51.078(-5)?  at checkSubtree (/Users/srichardson/AppDevNew/ve-client-new/.meteor/local/build/programs/server/packages/check.js:177:13) 
I20150710-07:56:51.078(-5)?  at /Users/srichardson/AppDevNew/ve-client-new/.meteor/local/build/programs/server/packages/check.js:314:9 
I20150710-07:56:51.078(-5)?  at Function._.each._.forEach (/Users/srichardson/AppDevNew/ve-client-new/.meteor/local/build/programs/server/packages/underscore.js:147:22) 
I20150710-07:56:51.078(-5)?  at checkSubtree (/Users/srichardson/AppDevNew/ve-client-new/.meteor/local/build/programs/server/packages/check.js:308:5) 
I20150710-07:56:51.078(-5)?  at check (/Users/srichardson/AppDevNew/ve-client-new/.meteor/local/build/programs/server/packages/check.js:50:5) 
I20150710-07:56:51.078(-5)?  at createUser (/Users/srichardson/AppDevNew/ve-client-new/.meteor/local/build/programs/server/packages/accounts-password.js:795:3) 
I20150710-07:56:51.078(-5)?  at Object.Accounts.createUser (/Users/srichardson/AppDevNew/ve-client-new/.meteor/local/build/programs/server/packages/accounts-password.js:875:10) 
I20150710-07:56:51.078(-5)?  at [object Object].Meteor.methods.registerNewUser (app/server/server.js:17:25) 
I20150710-07:56:51.078(-5)?  at [object Object].methodMap.(anonymous function) (/Users/srichardson/AppDevNew/ve-client-new/.meteor/local/build/programs/server/packages/meteorhacks_kadira.js:2536:30) 
I20150710-07:56:51.078(-5)?  at maybeAuditArgumentChecks (/Users/srichardson/AppDevNew/ve-client-new/.meteor/local/build/programs/server/packages/ddp.js:2445:12) 
I20150710-07:56:51.078(-5)?  at /Users/srichardson/AppDevNew/ve-client-new/.meteor/local/build/programs/server/packages/ddp.js:1476:20 
I20150710-07:56:51.078(-5)?  at [object Object]._.extend.withValue (/Users/srichardson/AppDevNew/ve-client-new/.meteor/local/build/programs/server/packages/meteor.js:989:17) 
I20150710-07:56:51.079(-5)?  at /Users/srichardson/AppDevNew/ve-client-new/.meteor/local/build/programs/server/packages/ddp.js:1475:41 
I20150710-07:56:51.079(-5)?  at [object Object]._.extend.withValue (/Users/srichardson/AppDevNew/ve-client-new/.meteor/local/build/programs/server/packages/meteor.js:989:17) 
I20150710-07:56:51.079(-5)?  at [object Object]._.extend.protocol_handlers.method (/Users/srichardson/AppDevNew/ve-client-new/.meteor/local/build/programs/server/packages/ddp.js:1474:51) 
I20150710-07:56:51.079(-5)?  at /Users/srichardson/AppDevNew/ve-client-new/.meteor/local/build/programs/server/packages/meteorhacks_kadira.js:2425:38 
I20150710-07:56:51.079(-5)? Sanitized and reported to the client as: Match failed [400] 

感謝很多的幫助。

回答

1

我不確定爲什麼你將數據作爲數組聲明時,你使用它作爲一個對象。

嘗試

var data = {}; 

代替。

我只是試圖做一個這個自己的準系統版本,我得到了同樣的問題,當我將它聲明爲一個有趣的數組。

在客戶端上,一切看起來很正常(一旦你開始使用密鑰,它就放棄數據是一個數組),但在服務器上,只要它擔心數據是一個空數組。奇怪的結果,但聲明數據作爲一個對象修復它。

+0

哇。就是這樣。不知道我在想什麼,但我錯過了它。感謝您指出。有趣的問題,是真的。 – imarichardson

相關問題