Reputation: 95
I tried to figure what's going wrong. But I could't fixed it, only guess what's the matter of my code.
I think it's problem of async, working process of nodejs. But I'm begginer of JS, I can't fix this problem.
So, here my code
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());
}
and this it Result
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
}
}
and result is always changing
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
}
}
plz help me.. It's I've been working with this problem for 5hours.
Upvotes: 0
Views: 88
Reputation: 933
let i variable is global and there is no local varible i so your i value is getting updated. Simple solution is to to change the name of global value from i to any other and define a new global variable i. To solve the problem introduce a new global variable and let i global variable available to be used as local inside class method.
Solution will be:
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());
}
Upvotes: 1
Reputation:
What someone commented above is that because you reuse the same variable (not just the same variable name, but the exact same variable) in the function scope and the global scope, you are iterating over the value twice in the while() loop and for() loop. This means that you get the first value of i correctly, but then the loop in the function is incrementing it before the outer loop is finished, so that the second result is already 7.
In general you should try to define variables in a way that limits them to the current scope (loop, function, etc). Instead of running the while() loop in the global scope, you can create a function main() which runs the loop and define i there.
function main() {
let i = 0;
while (...) {
...
}
}
main();
Even if you didn't do this, so long as you define i within the function with the for() loop, you avoid retrieving and modifying its value globally.
In general I recommend using the "use strict"; line at the beginning of your scripts to avoid this kind of issue which is caused by not having to define variables. If you use my example above, you would get an error using for(i=0...) because i is not defined. Anyway you should use this in the tagsGen() function:
for(let i=0;...) {
...
}
Upvotes: 3