尝试使用 Marak/faker.js 导入数据时出错

Dee*_*lux 4 javascript faker meteor reactjs

我似乎找不到我的问题。有人看到我做错了什么吗?这个项目是用 Meteor 和 React 制作的。

我的导入文件:

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');
			    }
			});
		});
	}
});
Run Code Online (Sandbox Code Playgroud)

这是我用来写评论的方法文件:

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,
});
Run Code Online (Sandbox Code Playgroud)

这是我在终端中得到的错误代码:TypeError:无法读取未定义的属性“lorem”。

任何帮助都受到高度赞赏。

编辑:

正如建议的那样,我对从“import { lorem, faker } from 'faker';”导入的内容进行了更改。“从‘faker’导入faker;”

我也改变了这个“faker.lorem.title();” 到“faker.hacker.noun();”

谢谢贵格!

Gui*_*uig 6

看起来Faker 是faker作为默认导出的,而不是作为常量导出的。所以你应该做

import faker from 'faker';
// then use `faker.lorem` as you are currently doing
Run Code Online (Sandbox Code Playgroud)

或者

import { lorem } from 'faker';
// then use `lorem` instead of `faker.lorem`
Run Code Online (Sandbox Code Playgroud)

目前,你正在做

import { lorem, faker } from 'faker';
Run Code Online (Sandbox Code Playgroud)

然后使用faker.lorem,因此lorem不使用您正在导入的。并且faker您尝试导入的是未定义的,因此调用faker.lorem(...会引发异常错误TypeError: Cannot read property 'lorem' of undefined.