2014-03-19 1 views
0

메인 페이지를로드하기 전에 유효성 검사를 일부 수행하려고합니다. 이 작업을 수행하려면 확인한 문서를 Mongo Collection에 있어야합니다. 불행하게도 client.js에서 문서를 찾는 것은 효과가없는 것 같습니다. 제 생각에는 클라이언트와 서버 콜렉션이 동기화되어 있지 않습니다. 비슷한 기사를 토대로 필자는 많은 성공을 거두지 못했다. 다음은 내가 시도한 것을 요약 한 것입니다.meteor findOne 또는 find returns undefined

옵션 1 : 자동 구독을 사용하지 않고 클라이언트 측에서 레코드를 찾으십시오. 레코드를 찾을 수 없습니다. /client/app.js /server/server.js

crs_collection = new Meteor.Collection("crs"); 

Meteor.publish("crs", function(){ 
    return crs_collection.find(); 
}); 

옵션 2에서

crs_collection = new Meteor.Collection("crs"); 
Meteor.subscribe("crs"); 

Meteor.findrec = function(credentialToken) { 
    target = {credentialtoken:credentialToken}; 
    recfound = crs_collection.findOne(target); 

    //No luck with find either. 
    //recfound = crs_collection.find({credentialtoken:credentialToken}, {limit:1}).fetch()[0]; 
    console.log("recfound:",recfound); //returns recfound is undefined. 
    return recfound; 
} 

에서 app.js

credentialToken = "2KcNCRzpTHzyZ1111"; 

if (Meteor.isClient) { 
    Template.hello.greeting = function() { 
    return "Welcome to ares_sso."; 
    }; 

    Meteor.startup(function() {  
    var results = Meteor.findrec(credentialToken); 
    console.log("results:",results); //results is undefined. 
    }); 

    Template.hello.events({ 
    'click input': function() { 
     if (typeof console !== 'undefined') 
      console.log("You pressed the button"); 
    } 
    }); 

} 

if (Meteor.isServer) { 
    Meteor.startup(function() { 
    // code to run on server at startup 
    }); 
} 

에서

: 나는 찾기를 한 다음 서버 측에서는 작동하는 "server_recfind"메서드를 사용하지만 클라이언트에 컨텐트를 가져올 수 없습니다. /client/app.js /server/app.js

crs_collection = new Meteor.Collection("crs"); 

Meteor.publish("crs", function(){ 
    return crs_collection.find(); 
}); 

// Using Sync which finds the record but how do I sent the content to the client? 
Meteor.methods ({ 
    'server_findrec': function(credentialToken) { 
    // tried unblock but didnt work 
    //this.unblock(); 
    var rec = crs_collection.findOne({'credentialtoken': credentialToken}); 
    console.log("INSIDE server findrec rec=",rec); //shows content found 
    // tried flush but it didn't do anything 
    crs_collection.flush; 
    return rec; //rec not returning to the client 
    } 
}) 

옵션 3에서

crs_collection = new Meteor.Collection("crs"); 
Meteor.subscribe("crs"); 

에서 app.js

credentialToken = "2KcNCRzpTHzyZ1111"; 

if (Meteor.isClient) { 
    Template.hello.greeting = function() { 
    return "Welcome to ares_sso."; 
    }; 

    Meteor.startup(function() {  
    var results = Meteor.call('server_findrec',credentialToken); 
    console.log("results=",results); // also returns undefined 

    }); 

    Template.hello.events({ 
    'click input': function() { 
     if (typeof console !== 'undefined') 
      console.log("You pressed the button"); 
    } 
    }); 

} 

if (Meteor.isServer) { 
    Meteor.startup(function() { 
    // code to run on server at startup 
    }); 
} 

에서

: 좌절 그리고 난 이후 서버 메소드로 문서 레코드를 찾을 수 있습니다. 클라이언트 측에 컨텐트를 전달하기 위해 전역 변수를 추가하려고 시도했습니다. 불행하게도 그것은 /client/app.js에서 app.js

credentialToken = "2KcNCRzpTHzyZ1111"; 

//added global variables 
c1 = ''; 
c2 = ''; 
c3 = '' 

if (Meteor.isClient) { 
    Template.hello.greeting = function() { 
    return "Welcome to ares_sso."; 
    }; 

    Meteor.startup(function() {  
    var results = Meteor.call('server_findrec',credentialToken); 
    console.log("results=",results); // also returns undefined 
    console.log("c1=",c1); 
    console.log("c2=",c2); 
    console.log("c3=",c3); 
    }); 

    Template.hello.events({ 
    'click input': function() { 
     if (typeof console !== 'undefined') 
      console.log("You pressed the button"); 
    } 
    }); 

} 

if (Meteor.isServer) { 
    Meteor.startup(function() { 
    // code to run on server at startup 
    }); 
} 

에서

를 작동하지 않았다 /server/app.js에서

crs_collection = new Meteor.Collection("crs"); 
Meteor.subscribe("crs"); 

crs_collection = new Meteor.Collection("crs"); 

Meteor.publish("crs", function(){ 
    return crs_collection.find(); 
}); 

// Using Sync which finds the record but how do I sent the content to the client? 
Meteor.methods ({ 
'server_findrec': function(credentialToken) { 

    // tried unblock but didnt work 
    //this.unblock(); 

    var rec = crs_collection.findOne({'credentialtoken': credentialToken}); 
    console.log("INSIDE server findrec rec=",rec); //shows content found 
    c1 = rec.cont1; 
    c2 = rec.cont2; 
    c3 = rec.cont3; 

    //confirm that c1,c2 and c3 have content 
    console.log(In server_findrec c1=",c); //shows content 
    console.log(In server_findrec c2=",c2); //shows content 
    console.log(In server_findrec c3=",c3); //shows content 

    // tried flush to sync to client...didn't work 
    crs_collection.flush; 

    return rec; //rec not returning to the client 
    } 
}) 

훨씬 더 많은 코드입니다. 그래서 위의 모든 것을 모아서 제가 시도한 것과 내가하려고하는 것에 대한 명확한 그림을 얻을 수 있기를 바랍니다. 그 과정에서 실수를하면 유감입니다.

전반적으로 내가 뭘 잘못하고 있는지 알면 좋습니다. 나는 3 가지 시나리오가 효과가 있다고 믿는다. 어떤 도움이나 추천도 환영합니다.

저는 Meteor Release 0.7.1.2를 사용하고 CoffeeScript는 사용하지 않습니다.

는 두 가지 실수를하고 당신에게 모든

+0

다음을 확인하십시오. 해당 문서가 실제로 db에 있는지 확인 했습니까? db에 나타나는 질문과 정확히 일치하는 문서를 추가 할 수 있습니까? –

+0

예. 나는 확인했다. – user3225540

+0

다음은 컬렉션에 대한 쿼리입니다. DB는 다른 상자에서 호스팅됩니다. > db.crs.find() { "credentialtoken": "2KcNCRzpTHzyZ1111", "email": "[email protected]", "day": "18", "source": "pdp_sso", "_id": "CbXPCgx3nJJBvbzTD"} – user3225540

답변

1

감사합니다

  1. 한 번 crs_collection을 정의하고 클라이언트와 서버에서 실행되는 파일 이잖아 년을 확인하십시오. 그것은 전 세계적으로 정의되어야합니다.

  2. crs_collection 게시/하위 코드보다 먼저 정의해야합니다. Meteor는 먼저 lib 디렉토리에있는 파일을 실행하므로 콜렉션 코드를 넣는 것이 가장 좋습니다.

정말 그게 전부입니다. 필요한 경우 예제를 제공해 드리겠습니다.

+0

감사합니다. Jake. 나는 여전히 유성에 새로운 예가 될 것입니다. 처음에 crs_collection = new Meteor.Collection ("crs")을 app.js에 넣었고 동일한 문제가 발생했습니다. 얼마 후 유사한 게시물을 읽은 후 /client/app.js 및 /server/app.js로 변경했습니다. 나는 너의 권고를 시도 할 것이다. 다시 감사합니다. Vince – user3225540

+0

제이크, 도와 줘서 고마워. 불행히도 그것은 작동하지 않았다. 솔직히 내가 뭘 잘못하고 있는지, 나는 좌절하고 있는지 몰라. app.js 또는 /client/app.js에서 findOne()을 실행하면 작동하지 않는 것 같습니다. crs_collection 정의를 lib 디렉토리의 새 app.js 파일이나 기존 app.js의 맨 위로 이동하십시오. 그러나 Google 콘솔로 이동하여 동일한 findOne 문을 입력하면 작동합니다. Server/app.js에서 findOne은 항상 작동합니다. 문제는 전역 변수를 사용하여 결과를 반환 할 수 없다는 것입니다. 도와 주셔서 다시 한 번 감사드립니다. 빈스 – user3225540