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



我试图弄清楚出了什么问题。但我无法修复它,只能猜测我的代码有什么问题。

我认为这是 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 个小时了。

上面有人评论的是,由于您在函数作用域和全局作用域中重用相同的变量(不仅是相同的变量名,而是完全相同的变量(,因此您在 while(( 循环和 for(( 循环中迭代该值两次。这意味着您正确获得了 i 的第一个值,但随后函数中的循环在外部循环完成之前递增它,因此第二个结果已经是 7。

通常,您应该尝试以将变量限制在当前范围(循环、函数等(的方式定义变量。与其在全局范围内运行 while(( 循环,不如创建一个函数 main(( 来运行循环并在那里定义 i。

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

即使你没有这样做,只要你在函数中使用 for(( 循环定义 i,你就可以避免全局检索和修改其值。

一般来说,我建议在脚本开头使用"use strict"; 行,以避免这种由不必定义变量引起的问题。如果您使用上面的示例,则使用 for(i=0...( 时会出现错误,因为 i 未定义。无论如何,你应该在 tagsGen(( 函数中使用它:

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

leti 变量是全局变量,并且没有局部变量 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());
}