我已经使用Zapier几个星期了,按照正常情况,当我们构建每个功能时,我们的需求变得更加复杂。因此,我目前在“Zapier代码”中使用了一些JavaScript。在函数中使用fetch()会导致“输出未定义”
请注意任何答案:我自己和我提供的程序员团队不是JavaScript专家,但我们确实理解大多数概念。
目的:仅当javascript的输入为真时才进行fetch()调用。
问题:当fetch()被包含在代码中时,Zapier总是返回“output not defined”。
ReferenceError: output is not defined theFunction
ie:总的代码结构是好的,我使用fetch()和返回和处理的方式不是。
我已经尝试了一个gazillion变化,并在这里使用了一些很棒的帖子,但没有解决问题。
简化代码如下所示:
//This does the Fetch:
function doFetch(urlAddress) {
console.log("2. Doing Fetch");
return fetch(urlAddress)
.then(function(res) {
return res.text();
})
.then(function(response) {
//Hardcode a return.
console.log("3. Returning Response");
return response.json();
});
}
if (input.emailHasChanged == "true")
{
//Do Something
console.log("1. Updating Email");
doFetch(urlAddress)
.then(function(response) {
//Do something with Reponse.
console.log("4. Doing Something: ", response);
callback(null, response);
});
}
else
{
//Do Nothing
console.log("1. Not Updating email");
output = {id: 2};
}
我相信这是我无论是从fetch()方法返回的路上或JavaScript的异步特性。
注意:Zapier为我定义了'input'和'output'变量。 '输出'最初是空值,必须由代码设置一个值。
这里:'回调(NULL,输出);',你会引用未定义'output'变量,从而导致错误。你的意思是它是'回调(空,响应)'? – jfriend00
@ jfriend00对不起,我的错误简化了帖子的代码。固定。是的,我正在使用'callback(null,response)' –
哪行代码导致错误?仅供参考,当您执行'output = {id:2};','output'时仍未定义。这将创建一个隐式的全局,除了'strict'模式,它会导致一个错误。 – jfriend00