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

将变量定义为let和var,表示NaN

IT培训 admin 13浏览 0评论

将变量定义为let和var,表示NaN

我正在使用如下所示的变量:

var maxId=1;
        schema.table.max('id').then(function(max) {
        maxId=max+1;
        }).catch(err => {
          maxId=1;
        });

从数据库中获取最大ID并添加一个。

console.log(maxId) //prints the maximum id.

我也想通过以下方式为新数据添加新ID:

 schema.table.findAll({where: option}).then((existed) => {

                for (let insert of insertionList) {
                      insert['id'] = maxId;
                      maxId=maxId+1;
                }

            })

在这里,插入列表的id属性显示为NaN而不是最大值。

我使用let和var,但两者均导致NaN。任何帮助表示赞赏。

回答如下:
undefined + 1
=> NaN

我认为当您的数据库为空时,它不会通过catch语句,因为尝试获取最大值没有错误(即使它是undefined。]

您应该处理未定义max的情况。

这是我用ES6编写的解决方案:

let maxId = 1;
schema.table.max('id')
    .then(max => maxId = (max || 0) + 1)

将变量定义为let和var,表示NaN

我正在使用如下所示的变量:

var maxId=1;
        schema.table.max('id').then(function(max) {
        maxId=max+1;
        }).catch(err => {
          maxId=1;
        });

从数据库中获取最大ID并添加一个。

console.log(maxId) //prints the maximum id.

我也想通过以下方式为新数据添加新ID:

 schema.table.findAll({where: option}).then((existed) => {

                for (let insert of insertionList) {
                      insert['id'] = maxId;
                      maxId=maxId+1;
                }

            })

在这里,插入列表的id属性显示为NaN而不是最大值。

我使用let和var,但两者均导致NaN。任何帮助表示赞赏。

回答如下:
undefined + 1
=> NaN

我认为当您的数据库为空时,它不会通过catch语句,因为尝试获取最大值没有错误(即使它是undefined。]

您应该处理未定义max的情况。

这是我用ES6编写的解决方案:

let maxId = 1;
schema.table.max('id')
    .then(max => maxId = (max || 0) + 1)

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论