Meteor 프로젝트에서 작업 중이며 Meteor.call의 반환 값을 클라이언트 측의 템플릿 도우미에서 가져오고 싶습니다. 맨 처음 콜백 함수에서 변수를 설정하고 Meteor.call
외부에서 변수의 값을 가져옵니다. Meteor.call
코드가 전혀 실행되지 않으면 코드를 발견했습니다. 그런 다음 조금 검색하여 Session을 사용하면됩니다. 그러나 나는 그 이유를 정말로 모른다. 여기 내 원래 코드와 수정 된 코드가 있습니다. 아무도 나를 위해 조금 설명 할 수 있습니까? 감사!!클라이언트 측 비동기 Meteor.call을 이해하는 방법
원래 잘못된 코드 : HTML
<div id="text-result-main">
<h2>{{title}}</h2>
</div>
JS
Template.texts.helpers({
title: function(){
var index = Router.current().params.index;
Meteor.call('getTitle', index,function(error, result){
titles = result;
});
console.log(titles);
return titles;
}});
컬렉션 text.js
Text = new Mongo.Collection("text");
Meteor.methods({
'getTitle': function(myindex){
return Text.findOne({index: myindex}).title;
}});
작업 코드 : JS
Template.texts.helpers({
title: function(){
var index = Router.current().params.index;
Meteor.call('getTitle', index,function(error, result){
Session.set("titles",result);
});
console.log(Session.get("titles"));
return Session.get("titles");
}});
클라이언트에 콜렉션 텍스트를 게시하지 않았다는 것에 주목하십시오. 잘못된 코드를 실행할 때 페이지를 새로 고칠 때마다 "제목"의 내용을 보거나 콘솔에서 볼 수 없습니다. 그러나 세션을 설정하면 작동합니다. 나는 그것이 어떻게 여기에서 작동하는지 정말로 모른다. 감사합니다
(http://docs.meteor.com/api/methods.html#Meteor-call에서). – Quirk