TypeError: Cannot read property 'uuid' of undefined faker-js faker
Asked Answered
Z

2

6

I am trying to use faker-js package, but I unexpectedly get a TypeError: Cannot read property 'uuid' of undefined for my variable businessId which try to use faker.datatype.uuid()

It usually happen when I forget to install faker-js, but that's not the case here, and I check that I imported this package. Therefore I am clueless on what I am doing wrong here.

// eslint-disable-next-line import/order
const { initConfig } = require('../../../config');

initConfig();

const sinon = require('sinon');
const faker = require('@faker-js/faker');
const { retryAsyncCall } = require('../../../src/common/helpers/retry-async');
const { createFacebookAdVideoFromUrl } = require('../../../src/common/controllers/facebook/api');

function createPayloadDataBuilder(payload = {}) {
  const template = {
    accountId: faker.datatype.uuid(),
    publicLink: faker.internet.url(),
    videoName: faker.lorem.word(),
    facebookToken: undefined,
    params: null,
    businessId: faker.datatype.uuid(),
  };
  return { ...payload, ...template };
}

describe('Facebook Gateway', () => {
  describe('createFacebookAdVideoFromUrl', () => {
    describe('Given businessId', () => {
      it.only("should create the video calling business's Facebook ids", async () => {
        const businessId = faker.datatype.uuid();
        console.log(faker, businessId);
        const createFacebookAdVideoPayload = createPayloadDataBuilder({
          businessId,
        });

        await createFacebookAdVideoFromUrl(...createFacebookAdVideoPayload);

        // sinon.assert.called(retryAsyncCall);
      });
    });
  });
});
Zendavesta answered 6/5, 2022 at 15:20 Comment(3)
What is a problem to use crypto.randomUUID() to generate UUID?Galimatias
It's only a symptom, I have the same issue with all the sub-values of datatypeZendavesta
I just try with faker.internet.url(), same issue : TypeError: Cannot read property 'url' of undefinedZendavesta
Z
9

Found it! I need to destructure the import:

const { faker } = require('@faker-js/faker');

Zendavesta answered 6/5, 2022 at 16:48 Comment(3)
Sadly this does not fix the issue for mePropitiate
Fixed for me (using module.exports = function(){ return { // ... } })Schwann
It worked in my case, Thanks man.Casern
L
3

faker.random.uuid() has been moved to faker.datatype.uuid()

See the docs: https://fakerjs.dev/api/datatype.html#uuid

Lucais answered 30/1, 2023 at 17:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.