Js,循环正在工作-稀疏工作

问题描述 投票:0回答:2

我试图弄清楚会发生什么。但是我无法解决它,只能猜测我的代码有什么问题。

我认为这是nodejs异步工作过程的问题。但是我是JS的初学者,无法解决此问题。

所以,这里是我的代码

var faker = require('faker');

class Data {
        constructor(){
                this.postId = 0;
                this.bookId = 0;
                this.seriseId = 0;
                this.languageTags = ["C","C++","python","Java","Kotlin","JavaScript","C#","CSS","HTML"];
                this.frameworkTags = ["Django","React","Redux","Arduino","Graph QL","node","express","docker"];
                this.allTags = this.languageTags.concat(this.frameworkTags);
        }

        randomIntGen(min,max){
                return Math.floor(Math.random()*(max-min)+min);
        }
        tagGen(){
                const tag = this.allTags[this.randomIntGen(0,this.allTags.length)];
                return tag;
        }
        tagsGen(){
                const tagsNum = this.randomIntGen(0, this.allTags.length);
                const tags = new Set();
                for(i=0; i<tagsNum; i++){
                        const tagIndex = this.randomIntGen(0,this.allTags.length);
                        tags.add(this.allTags[tagIndex]);
                }
                return [...tags];
        }
        typeGen(){
                const types = ["series", "books","normal"];
                const typeIndex = this.randomIntGen(0,3);
                return types[typeIndex];
        }
        userGen(){
                const userId = faker.finance.bitcoinAddress();
                const userAuthor = faker.internet.userName();
                const userImg = faker.image.avatar();
                return {
                        userId,
                        userAuthor
                };
        }
        postGen(){
                const type = this.typeGen();
                const user = this.userGen();
                const tag = this.tagGen();
                const tags = this.tagsGen();
                const headers = function(){
                        return {
                                type,
                                category: tag,
                                image: faker.image.image(),
                                tags: tags,//we need to fix this part
                                title: faker.lorem.words(),
                                author: user.userAuthor,
                                data:{
                                        created: faker.date.past(),
                                        fixed: faker.date.recent(),
                              }                 
                       };
                };
                return headers();
        }


        dataGen(genOpt = {}){
                const user = this.userGen();
                const test = this.postGen();
                console.log("we tried");
                return test;
        }
}




let i = 0;
while(i<10){
    i++;
    console.log(i);
    const datajson = new Data();
    console.log(datajson.dataGen());
}

和此结果

1
we tried
{
  type: 'books',
  category: 'express',
  image: 'http://lorempixel.com/640/480/people',
  tags: [ 'JavaScript', 'Arduino', 'python', 'C', 'Redux' ],
  title: 'numquam enim fugiat',
  author: 'Eddie_Bartoletti31',
  data: {
    created: 2019-05-31T19:24:56.369Z,
    fixed: 2019-12-01T00:28:02.868Z
  }
}
7
we tried
{
  type: 'normal',
  category: 'Redux',
  image: 'http://lorempixel.com/640/480/fashion',
  tags: [
    'python', 'C',
    'Redux',  'express',
    'Kotlin', 'React',
    'C#',     'Graph QL',
    'docker', 'C++'
  ],
  title: 'ad neque reiciendis',
  author: 'Orie87',
  data: {
    created: 2019-07-28T13:54:18.529Z,
    fixed: 2019-12-01T10:19:44.623Z
  }
}

结果总是在变化

1
we tried
{
  type: 'normal',
  category: 'docker',
  image: 'http://lorempixel.com/640/480/food',
  tags: [
    'Java',     'docker',
    'Kotlin',   'python',
    'Graph QL', 'C#',
    'C',        'CSS',
    'node'
  ],
  title: 'omnis consequatur qui',
  author: 'Celine.Berge33',
  data: {
    created: 2019-10-14T08:57:48.329Z,
    fixed: 2019-11-30T23:33:11.333Z
  }
}

请帮助我。.我已经解决这个问题5个小时了。

javascript node.js
2个回答
2
投票

上面的评论是,因为您在函数作用域和全局作用域中重用了相同的变量(不仅是相同的变量名,而且是完全相同的变量),所以您在while()循环中对该值进行了两次迭代和for()循环。这意味着您正确地获得了i的第一个值,但是函数的循环在外循环完成之前将其递增,因此第二个结果已经为7。

通常,您应尝试以将变量限制为当前作用域(循环,函数等)的方式定义变量。您可以创建一个函数main()来运行循环并在其中定义i,而不是在全局范围内运行while()循环。

function main() {
    let i = 0;
    while (...) {
        ...
    }
}

main();

即使您没有执行此操作,只要您使用for()循环在函数中定义i,也可以避免全局检索和修改其值。

一般来说,我建议使用“严格使用”;请在脚本开头添加一行,以避免由于不必定义变量而导致的此类问题。如果使用上面的示例,由于未定义i,使用for(i = 0 ...)会出现错误。无论如何,您应该在tagsGen()函数中使用它:

for(let i=0;...) {
    ...
}

0
投票

let i变量是全局变量,并且没有局部变量i,因此您的i值正在更新。一种简单的解决方案是将全局值的名称从i更改为任何其他值,然后定义一个新的全局变量i。为了解决该问题,请引入一个新的全局变量,并让我将该全局变量用作局部内部类方法。

解决方案将是:

let i = 0;//This should be used tagsgen() method so don't used it any where else.
let j=0;//use new variable

while(j<10){
    j++;
    console.log(j);
    const datajson = new Data();
    console.log(datajson.dataGen());
}
© www.soinside.com 2019 - 2024. All rights reserved.