使用 useTracker 和渲染的 Meteor 方法调用 html
Meteor method call with useTracker and render html
这里是流星新手,我想对流星方法进行流星调用,该方法向第 3 方 API 发出获取请求,并将返回的 JSON 响应转发给我的初始流星调用。
我想使用返回的 JSON 响应来呈现我的 html 代码
这是我用流星方法的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)
如何获取流星呼叫的响应并仅更新一次 jiraResponse?
在方法回调中设置 jiraResp
将触发重新渲染,并且由于您是在渲染方法本身中进行方法调用,因此它将重复调用,因此循环。
你需要使用useEffect
:
const [ jiraResp, setjiraResp ] = useState({});
useEffect(() =>
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);
这里是流星新手,我想对流星方法进行流星调用,该方法向第 3 方 API 发出获取请求,并将返回的 JSON 响应转发给我的初始流星调用。
我想使用返回的 JSON 响应来呈现我的 html 代码
这是我用流星方法的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)
如何获取流星呼叫的响应并仅更新一次 jiraResponse?
在方法回调中设置 jiraResp
将触发重新渲染,并且由于您是在渲染方法本身中进行方法调用,因此它将重复调用,因此循环。
你需要使用useEffect
:
const [ jiraResp, setjiraResp ] = useState({});
useEffect(() =>
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);