最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

仅使用Node JS中包含数据的变量的正确方法是什么?

IT培训 admin 16浏览 0评论

仅使用Node JS中包含数据的变量的正确方法是什么?

我一直在Node JS和Swift中进行研究。我的iOS应用程序中有一个“表单”,为我提供以下3个选项:

  • 更新信用卡的到期日期
  • 更新信用卡的有效期
  • 完全更新默认信用卡

[在某些情况下,用户仅更新默认卡,仅更新日期之一或两者都更新,而不更新默认卡。两个date.etc,任何可能的更新场景。

我的问题是,如何允许该函数接受空白或空输入?我知道我可以将CONST变量设置为null开头,但是如何防止将它提交给stripe作为空白并清除数据或错误?例如,如果const newdefault为空白,如何确保default_source不会被空白数据更新?

我将使用filter命令并将它们放入数组吗?这些已经在JSON字符串中,所以我不确定数组是否可以工作。另外,如果所有3个均为空白(表示没有人更新到期的月份或年份,或者没有选择默认值,那么它们将无法执行该功能,我的Swift代码中有一个null检查器)

请参见下面带有注释的代码::

exports.updateCard = functions.https.onCall((data, context) => {
    const stripeId = data.stripeId;  // needed as users stripe ID
    const cardId = data.cardId;      // the current card they are updating, comes from the app
    const month = data.expmonth;    // this may or may not be blank
    const year = data.expyear;      // this may or may not be blank
    const newdefault = data.newdefault;  //this may or may not be blank

    return stripe.customers.updateSource(stripeId, cardId,
        {
            exp_month: month,  <--- how can I make sure this is not updated if month is blank?
            exp_year: year  <--- how can I make sure this is not updated if year is blank?
        }
    ).then(() => {
        return stripe.customers.update(stripeId,
            {
                default_source: newdefault <--- how can I make sure this is not updated if newdefault is blank?
            }
        ).then(() => {
            console.log(card);
            return { myReturn: card };
        }).catch((err) => {
            console.log(err);
        });
    });
});

在我的研究中,我发现有一个名为array.filter的命令可以使用。但是,这些不是数组,而是JSON字符串。这是我在StackOverflow和链接上找到的示例。StackOverflow article on .filter

var array = [0, 1, null, 2, "", 3, undefined, 3,,,,,, 4,, 4,, 5,, 6,,,,];

var filtered = array.filter(function (el) {
  return el != null;
});

console.log(filtered);

有人可以建议我是否可以使用此方法,如果不能,请提供一个示例,说明我可以使用该方法来实现仅在变量具有数据的情况下才允许提交条纹。可能是switch语句?

回答如下:

如果您仅根据所需的显式条件使用属性填充对象,则可能会更直接。

const obj = {}
if (thing) {
    obj.thing = thing
}
if (otherthing) {
    obj.otherthing = otherthing
}

stripe.customers.updateSource(obj)

换句话说,当您调用任何API时,无需一次指定整个对象。只需根据所需条件构建对象。

仅使用Node JS中包含数据的变量的正确方法是什么?

我一直在Node JS和Swift中进行研究。我的iOS应用程序中有一个“表单”,为我提供以下3个选项:

  • 更新信用卡的到期日期
  • 更新信用卡的有效期
  • 完全更新默认信用卡

[在某些情况下,用户仅更新默认卡,仅更新日期之一或两者都更新,而不更新默认卡。两个date.etc,任何可能的更新场景。

我的问题是,如何允许该函数接受空白或空输入?我知道我可以将CONST变量设置为null开头,但是如何防止将它提交给stripe作为空白并清除数据或错误?例如,如果const newdefault为空白,如何确保default_source不会被空白数据更新?

我将使用filter命令并将它们放入数组吗?这些已经在JSON字符串中,所以我不确定数组是否可以工作。另外,如果所有3个均为空白(表示没有人更新到期的月份或年份,或者没有选择默认值,那么它们将无法执行该功能,我的Swift代码中有一个null检查器)

请参见下面带有注释的代码::

exports.updateCard = functions.https.onCall((data, context) => {
    const stripeId = data.stripeId;  // needed as users stripe ID
    const cardId = data.cardId;      // the current card they are updating, comes from the app
    const month = data.expmonth;    // this may or may not be blank
    const year = data.expyear;      // this may or may not be blank
    const newdefault = data.newdefault;  //this may or may not be blank

    return stripe.customers.updateSource(stripeId, cardId,
        {
            exp_month: month,  <--- how can I make sure this is not updated if month is blank?
            exp_year: year  <--- how can I make sure this is not updated if year is blank?
        }
    ).then(() => {
        return stripe.customers.update(stripeId,
            {
                default_source: newdefault <--- how can I make sure this is not updated if newdefault is blank?
            }
        ).then(() => {
            console.log(card);
            return { myReturn: card };
        }).catch((err) => {
            console.log(err);
        });
    });
});

在我的研究中,我发现有一个名为array.filter的命令可以使用。但是,这些不是数组,而是JSON字符串。这是我在StackOverflow和链接上找到的示例。StackOverflow article on .filter

var array = [0, 1, null, 2, "", 3, undefined, 3,,,,,, 4,, 4,, 5,, 6,,,,];

var filtered = array.filter(function (el) {
  return el != null;
});

console.log(filtered);

有人可以建议我是否可以使用此方法,如果不能,请提供一个示例,说明我可以使用该方法来实现仅在变量具有数据的情况下才允许提交条纹。可能是switch语句?

回答如下:

如果您仅根据所需的显式条件使用属性填充对象,则可能会更直接。

const obj = {}
if (thing) {
    obj.thing = thing
}
if (otherthing) {
    obj.otherthing = otherthing
}

stripe.customers.updateSource(obj)

换句话说,当您调用任何API时,无需一次指定整个对象。只需根据所需条件构建对象。

发布评论

评论列表 (0)

  1. 暂无评论