最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - TypeError: Cannot read property 'uuid' of undefined faker-js faker - Stack Overflow

programmeradmin5浏览0评论

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/mon/helpers/retry-async');
const { createFacebookAdVideoFromUrl } = require('../../../src/mon/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);
      });
    });
  });
});

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/mon/helpers/retry-async');
const { createFacebookAdVideoFromUrl } = require('../../../src/mon/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);
      });
    });
  });
});
Share Improve this question asked May 6, 2022 at 15:20 A MehmetoA Mehmeto 1,9994 gold badges29 silver badges47 bronze badges 3
  • What is a problem to use crypto.randomUUID() to generate UUID? – NNL993 Commented May 6, 2022 at 15:23
  • It's only a symptom, I have the same issue with all the sub-values of datatype – A Mehmeto Commented May 6, 2022 at 15:25
  • I just try with faker.internet.url(), same issue : TypeError: Cannot read property 'url' of undefined – A Mehmeto Commented May 6, 2022 at 15:27
Add a ment  | 

3 Answers 3

Reset to default 9

Found it! I need to destructure the import:

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

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

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

Looks like the latest version (v9.4.0) has moved this again to faker.string.uuid()

https://fakerjs.dev/api/string.html#uuid

发布评论

评论列表(0)

  1. 暂无评论