admin管理员组

文章数量:1353252

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/ments/ments';
import { insertComment } from '../../api/ments/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');
			    }
			});
		});
	}
});

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/ments/ments';
import { insertComment } from '../../api/ments/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 ment:

import { Comments } from './ments';
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: 'ments.insert',
  validate: new SimpleSchema({
    title: { type: String },
    content: { type: String },
  }).validator(),
  run(ment) {
    Comments.insert(ment);
  },
});

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!

Share Improve this question edited Sep 3, 2016 at 9:48 Deelux asked Sep 1, 2016 at 20:03 DeeluxDeelux 1,1952 gold badges12 silver badges26 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

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.

本文标签: javascriptError when trying to import data with MarakfakerjsStack Overflow