2013-12-18 172 views
2

我新來的流星,我想做一個使用流星的註冊表格,我卡在findOnesubscribe/publish。 這是我的`collection.js常見:流星訂閱undefined

User_Customer = new Meteor.Collection("USER_CUSTOMER"); 
Customer = new Meteor.Collection("CUSTOMER"); 

這是我publish.js/server

Meteor.publish("CUSTOMER", function() { 
    return Customer.find(); 
}); 

Meteor.publish("USER_CUSTOMER", function() { 
    return User_Customer.find(); 
}); 

,這是我的功能來訂閱和findOne /client

function isAvailableForUserRegister(email,identiy,phone){ 
    Meteor.subscribe("CUSTOMER", function(){ 
     var cust = null; 
     cust = Customer.findOne({Identity: identiy}); 
     if(cust != null){ 
      Meteor.subscribe("USER_CUSTOMER", function(){ 
       var uc1 = null; 
       uc1 = User_Customer.findOne({Email: email}); 
       if(uc1 != null){ 
        var uc2 = null; 
        uc2 = User_Customer.findOne({Phone: phone}); 
        if(uc2 != null) 
         return true 
        else 
         return false; 
       }else return false; 
      }); 
     }else return false; 
    }); 
} 

此功能isAvailableForUserRegister將返回,如果電子郵件/身份證/電話已存在集合中:

Identity in Customer 
Email in User_Customer 
Phone in User_Customer 

和問題的過程無法繼續進入第二認購後,我做的斷點與我的鉻,custundefined值。 請幫幫我,該如何解決這個功能?

感謝

回答

0

你需要調用Meteor.subscribe只有在客戶端內的一次。

客戶端代碼:

Meteor.subscribe("CUSTOMER"); 
Meteor.subscribe("USER_CUSTOMER"); 

function isAvailableForUserRegister(email,identiy,phone){ 
    var cust = Customer.findOne({Identity: identiy}); 
    if(cust === null) { 
    return false; 
    } 
    var uc1 = User_Customer.findOne({$or: [ 
    {"Email": email}, 
    {"Phone": phone} 
    ]); 
    return uc1 !== null;     
} 
+0

感謝答案,我試試您的解決方案,但它仍然未定義 – yozawiratama

1

我認爲這是一個異步的問題,訂閱還沒有準備好,試試這個

Meteor.subscribe("CUSTOMER").ready(); 
 
Meteor.subscribe("USER_CUSTOMER").ready();