메인 페이지를로드하기 전에 유효성 검사를 일부 수행하려고합니다. 이 작업을 수행하려면 확인한 문서를 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는 사용하지 않습니다.
는 두 가지 실수를하고 당신에게 모든
다음을 확인하십시오. 해당 문서가 실제로 db에 있는지 확인 했습니까? db에 나타나는 질문과 정확히 일치하는 문서를 추가 할 수 있습니까? –
예. 나는 확인했다. – user3225540
다음은 컬렉션에 대한 쿼리입니다. DB는 다른 상자에서 호스팅됩니다. > db.crs.find() { "credentialtoken": "2KcNCRzpTHzyZ1111", "email": "[email protected]", "day": "18", "source": "pdp_sso", "_id": "CbXPCgx3nJJBvbzTD"} – user3225540