Sergino
Sergino

Reputation: 10818

Faker.fake() does not work if input string has any params

I am trying to implement some data generation with fakejs, but faker.fake does not work if my template has any params here is the results:

const c = faker.fake('{{random.number({ min: 0, max: 10, precision: 1 })}}'); //does not work, getting NaN
const z = faker.fake('{{date.between("2015-01-01", "2015-12-31")}}'); //does not work, getting 'invalid date'

However this works:

const a = faker.fake('{{random.number}}'); //works
const b = faker.random.number({ min: 0, max: 10, precision: 1 }); //works

const x = faker.date.past(); // works
const y = faker.fake('{{date.past}}'); // works

Any idea how to fix? I am using fakerjs version 4.1.0

Upvotes: 0

Views: 795

Answers (1)

Nikita Skrebets
Nikita Skrebets

Reputation: 1538

Faker could not parse the parameters that it requires to be in JSON. So the parameters have to be in double quotes.

It doesn't log warnings about it, just assumes it's a string (fake function part below):

try {
  params = JSON.parse(parameters)
} catch (err) {
  // since JSON.parse threw an error, assume parameters was actually a string
  params = parameters;
}

This worked for me.

faker.fake('{{random.number({ "min": 0, "max": 10, "precision": 1 })}}');

Upvotes: 1

Related Questions