Deelux Deelux - 3 months ago 41
React JSX Question

Error when trying to import data with Marak/faker.js

I can't seem to find my problem. Anyone see what I am doing wrong?
This project is made with Meteor and React.

My import file:



import _ from 'lodash';
import { lorem, faker } from 'faker';
import { Comments } from '../../api/comments/comments';
import { insertComment } from '../../api/comments/methods.js';
import { Bert } from 'meteor/themeteorchef:bert';


Meteor.startup(() => {
// Great place to generate some data

// Check to see if data excists in the collection
// See if the collection has any records
const numberRecords = Comments.find({}).count();
if (!numberRecords) {
// Generate some data...
_.times(100, () => {
const title = faker.lorem.title();
const content = faker.lorem.title();

insertComment.call({
title, content,
}, (error) => {
if (error) {
Bert.alert(error.reason, 'danger');
} else {
target.value = '';
Bert.alert('Comment added!', 'success');
}
});
});
}
});





And this is the method file I use to write the comment:



import { Comments } from './comments';
import { SimpleSchema } from 'meteor/aldeed:simple-schema';
import { ValidatedMethod } from 'meteor/mdg:validated-method';
import { rateLimit } from '../../modules/rate-limit.js';

export const insertComment = new ValidatedMethod({
name: 'comments.insert',
validate: new SimpleSchema({
title: { type: String },
content: { type: String },
}).validator(),
run(comment) {
Comments.insert(comment);
},
});

rateLimit({
methods: [
insertComment,

],
limit: 5,
timeRange: 1000,
});





This is the error code I am getting in my terminal:
TypeError: Cannot read property 'lorem' of undefined.

Any help is highly appreciated.

EDIT:

As suggested I made the changes to the import from "import { lorem, faker } from 'faker';" to "import faker from 'faker';"

I also changed this "faker.lorem.title();" to "faker.hacker.noun();"

Thanks Guig!

Answer

It looks like Faker is exporting faker as a default, and not as a constant. So you should do

import faker from 'faker';
// then use `faker.lorem` as you are currently doing

or

import { lorem } from 'faker';
// then use `lorem` instead of `faker.lorem`

Currently, you are doing

import { lorem, faker } from 'faker';

and then using faker.lorem, so the lorem you're importing is not used. And the faker you are trying to import is undefined, so calling faker.lorem(... throws an error TypeError: Cannot read property 'lorem' of undefined. as excepted.