Javascript 如果条件不满足,函数中不返回任何内容?

Javascript 如果条件不满足,函数中不返回任何内容?,javascript,json,function,callback,zapier,Javascript,Json,Function,Callback,Zapier,我一直在使用Zapier的代码步骤编写代码,将变量信息发送到同一个Webhook。我知道了当需要发送信息时该怎么做,但是我只希望代码在购买条件为真时将对象发送到Webhook。如果buy不正确,我希望它不返回任何内容 if(coins[i].buy===true) { fetch('https://hooks.zapier.com/hooks/catch/974762/krbqch/', { method: 'POST', body: "Coin:"+coins[i].coin +",Valu

我一直在使用Zapier的代码步骤编写代码,将变量信息发送到同一个Webhook。我知道了当需要发送信息时该怎么做,但是我只希望代码在购买条件为真时将对象发送到Webhook。如果
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);
}

如果
buy
条件为false,我将得到一个错误“error:You must return a single object or array of objects.”这一点很重要,因为大多数时候
buy
都是false。我意识到这是因为没有回调,我只是不知道该放什么。因此,如果
buy
为false,为了防止出现错误,我应该返回什么?

根据错误,
error:您必须返回单个对象或对象数组。

Zapier希望您在最后返回一个对象或对象数组

您正在使用此语句对
buy===true
条件执行此操作-
callback(callback(null,{}))

尝试在条件块外执行相同的操作,只需添加相同的行-
回调(null,{})

这将把
{}
返回给Zapier,即使条件为false,代码步骤也应该成功

您的代码如下所示

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, {});
更多示例如下:

您可能希望获得一个新的webhook URL,因为它现在是公共的,任何人都可以触发它

我建议使用下面的代码块-(注意
回调
语句的更改,并删除一个
。然后
块。)


您是否尝试过使用
return false
在if语句之后,还有一件事javascript假设一切都是真的,除非它是假的,您可以像这样评估条件
如果(coins[i].buy)
,您不必===true@anthony-Trischiti如果这个答案解决了您的问题,请确保向上投票并将其选择为正确()
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, {});