2017-09-26 107 views
0

在註冊新用戶之前,我需要刪除前一個包含該電子郵件的用戶,並查詢API以將更多信息填入用戶作爲產品需求。Accounts.createUser中的流星異步呼叫

不幸的是我無法實現它,這是我從服務器得到的錯誤:Exception while invoking method 'createUser' Error: insert requires an argument

我做什麼,到目前爲止是這樣的:

客戶:

Accounts.createUser({ email, password, profile: { something } }, (err) => { 
    if (err) { 
    console.error(err2.reason); 
    } 
    history.replace('/account'); 
}); 

服務器:

Accounts.onCreateUser((options, user) => { 
    Meteor.users.remove({ email: options.email },() => { 
    try { 
     const res = request.postSync(authenticate, { 
     method: 'POST', 
     json: true, 
     body: { 
      email: options.email, 
      password: options.profile.password 
     } 
     }); 

     if (res.response.statusCode < 300) { 
     const newUser = user; 
     newUser.profile = {}; 
     newUser.profile.user_id = res.body.response.user_id; 
     newUser.profile.token = res.body.response.token; 
     return newUser; 
     } 

     throw new Meteor.Error(res.response.body.error.message); 
    } catch (err) { 
     throw new Meteor.Error(err.message); 
    } 
    }); 
}); 

我做錯了嗎?感謝

回答

0

Accounts.onCreateUser documentation

該函數應返回用戶文檔(或者一個傳入或新創建的對象)與任何修改都要求。返回的文檔直接插入到Meteor.users集合中。

你函數雖然沒有返回任何東西,但它只是調用Meteor.users.remove()作爲第二個參數的函數。不要忘了,DB電話是流星同步,所以它應該是這樣的:

Accounts.onCreateUser((options, user) => { 
    Meteor.users.remove({ email: options.email }); 
    // do something else 
    return user; 
}); 
+0

真棒,好知道DB調用是同步的。謝謝 –