[英]Returning nothing in function if condition not met?
I've been using Zapier's Code Steps to write code that sends variable information to the same Webhook. 我一直在使用Zapier的代码步骤来编写将变量信息发送到同一Webhook的代码。 I figured out how to do that when information needs to be sent however I only want the code to send the object(s) to the Webhook when the buy condition is true.
我想出了当需要发送信息时该怎么做的方法,但是我只希望代码在购买条件为true时将对象发送到Webhook。 If
buy
is not true I want it to return nothing. 如果
buy
不正确,我希望它不返回任何东西。
if(coins[i].buy===true)
{
fetch('https://hooks.zapier.com/hooks/catch/974762/krbqch/', { method:
'POST', body: "Coin:"+coins[i].coin +",Value:"+coins[i].currentValue
+",Buy:" +coins[i].buy+",Sell:"+coins[i].sell+",Date:"+currentDate})
.then(function(res) {
return res.json();
}).then(function(json) {
console.log(json);
}).then(function() {
callback(callback(null, {}));
})
.catch(callback);
}
If the buy
condition is false I will get an error of "Error: You must return a single object or array of objects." 如果
buy
条件为假,则将出现错误“错误:您必须返回单个对象或对象数组”。 This is important because most of the time buy
will be false. 这很重要,因为在大多数情况下
buy
都是错误的。 I realize that this is because there is no callback I just do not know what to put instead. 我意识到这是因为没有回调,我只是不知道要放什么。 So what should I return if
buy
is false in order to prevent an error? 因此,如果
buy
错误则应该退回什么东西以防止出错?
Going by the error, Error: You must return a single object or array of objects.
出现错误,
Error: You must return a single object or array of objects.
Zapier expects you to return an object or array of objects at the end. Zapier希望您最后返回一个对象或对象数组。
You are doing that for the buy === true
condition with this statement - callback(callback(null, {}));
您可以通过以下语句针对
buy === true
条件执行此操作callback(callback(null, {}));
Try doing the same outside the conditional block, just add the same line - callback(null, {});
尝试在条件块外执行相同的操作,只需添加同一行
callback(null, {});
This will return the {}
to Zapier and your code step should succeed even when the condition is false. 这会将
{}
返回给Zapier,即使条件为false,您的代码步骤也应该成功。
Your code would look like this 您的代码如下所示
if(coins[i].buy===true)
{
fetch('https://hooks.zapier.com/hooks/catch/974762/krbqch/', { method:
'POST', body: "Coin:"+coins[i].coin +",Value:"+coins[i].currentValue
+",Buy:" +coins[i].buy+",Sell:"+coins[i].sell+",Date:"+currentDate})
.then(function(res) {
return res.json();
}).then(function(json) {
console.log(json);
}).then(function() {
callback(callback(null, {}));
})
.catch(callback);
}
callback(null, {});
More examples here: https://zapier.com/help/code-examples/#introductory-http-example 此处有更多示例: https : //zapier.com/help/code-examples/#introductory-http-example
You might want to get a new webhook URL as it is now public and anyone can trigger it. 您可能想获取一个新的Webhook URL,因为它现在是公共的,任何人都可以触发它。
I would recommend using the code block below - (Notice changes to the callback
statement and removal of one .then
block.) 我建议使用下面的代码块-(注意更改
callback
语句并删除一个.then
块。)
if(coins[i].buy===true)
{
fetch('https://hooks.zapier.com/hooks/catch/974762/krbqch/', { method:
'POST', body: "Coin:"+coins[i].coin +",Value:"+coins[i].currentValue
+",Buy:" +coins[i].buy+",Sell:"+coins[i].sell+",Date:"+currentDate})
.then(function(res) {
return res.json();
}).then(function(json) {
console.log(json);
callback(null, {});
}).catch(callback);
}
callback(null, {});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.