Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/477.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用useTracker和render html调用Meteor方法_Javascript_Reactjs_Meteor_Use State - Fatal编程技术网

Javascript 使用useTracker和render html调用Meteor方法

Javascript 使用useTracker和render html调用Meteor方法,javascript,reactjs,meteor,use-state,Javascript,Reactjs,Meteor,Use State,Meteor新手,我想对Meteor方法进行Meteor调用,该方法向第三方API发出get请求,并将返回的JSON响应转发给我的初始Meteor调用 我想使用返回的JSON响应来呈现我的html代码 这是我使用meteor方法的js文件 Meteor.methods({ 'jira.get'(projectName, maxResult) { if (!this.userId) { throw new Meteor.Error('Not authorized.');

Meteor新手,我想对Meteor方法进行Meteor调用,该方法向第三方API发出get请求,并将返回的JSON响应转发给我的初始Meteor调用

我想使用返回的JSON响应来呈现我的html代码

这是我使用meteor方法的js文件

Meteor.methods({
  'jira.get'(projectName, maxResult) {
    if (!this.userId) {
      throw new Meteor.Error('Not authorized.');
    }
    check(projectName, String);
    check(maxResult, String);
    const base64Auth = Meteor.users.findOne({ _id: this.userId }).Authorization;

    const options = {
      method: 'GET',
      url: `https://myCompany.com/.../search?jql=project=${projectName}&maxResults=${maxResult}`,
      headers: {
        Authorization: base64Auth,
      },
    };

    const future = new Future();

    request(options, function(error, response) {
      if (error) throw new Error(error);
      const jiraResponse = JSON.parse(response.body);
      future.return(jiraResponse);
    });

    return future.wait();
  },
});
我调用上述Meteor方法的JSX文件如下

export const App = () => {
   
    Meteor.call('jira.get', project='test', maxResult = '10', (error, jiraResponse) => {
      console.log("this is working fine: "jiraResponse)
    });

    console.log('this is undefined', jiraResponse)
}
如果我像下面那样使用useState,那么最初最后一个控制台日志
console.log(jiraResp)
会像预期的那样打印
{}
,但之后它会进入无限循环,并带有正确的数据

 const [ jiraResp, setjiraResp ] = useState({})
  Meteor.call('jira.get', project='test', maxResult = '10', (error, jiraResponse) => {
    if (jiraResponse){
      console.log("got the resp ")
      setjiraResp(jiraResponse);
    }
    else{
      console.log("not recieved")
    }
  });
  console.log(jiraResp)

如何获得meteor调用的响应并只更新一次我的jiraResponse?

在方法回调中设置
jiraResp
将触发重新渲染,并且由于您在渲染方法本身中进行方法调用,因此它将重复调用,从而产生循环

您需要使用
useffect

const[jiraResp,setjiraResp]=useState({});
useffect(()=>
Meteor.call('jira.get',project='test',maxResult='10',(error,jiraResponse)=>{
如果(jiraResponse){
日志(“得到响应”);
setjiraResp(jiraResponse);
}否则{
控制台日志(“未收到”);
}
}), []);
console.log(jiraResp);

在方法回调中设置
jiraResp
将触发重新渲染,并且由于您在渲染方法本身中进行方法调用,因此它将重复调用,从而导致循环

您需要使用
useffect

const[jiraResp,setjiraResp]=useState({});
useffect(()=>
Meteor.call('jira.get',project='test',maxResult='10',(error,jiraResponse)=>{
如果(jiraResponse){
日志(“得到响应”);
setjiraResp(jiraResponse);
}否则{
控制台日志(“未收到”);
}
}), []);
console.log(jiraResp);

嘿,这真管用!谢谢你的工作!谢谢