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

标签 javascript meteor reactjs faker

我似乎找不到我的问题。有人看到我做错了什么吗? 这个项目是用 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');
			    }
			});
		});
	}
});

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

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,
});

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

非常感谢任何帮助。

编辑:

按照建议,我对来自“import { lorem, faker } from 'faker';”的导入进行了更改。 “从‘faker’导入 faker;”

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

谢谢吉格!

最佳答案

It looks like Faker 正在导出 faker 作为默认值,而不是常量。所以你应该这样做

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

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

目前,你正在做

import { lorem, faker } from 'faker';

然后使用 faker.lorem,因此不会使用您导入的 lorem。并且您尝试导入的 faker 未定义,因此调用 faker.lorem(... 会抛出错误 TypeError: Cannot read property 'lorem' of undefined . 作为异常(exception)。

关于javascript - 尝试使用 Marak/faker.js 导入数据时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39280020/

相关文章:

javascript - 我在网络输入中的正则表达式有什么问题?

Javascript OOP - 从对象方法(嵌套函数)内的函数返回值

javascript - 在 Meteor 中,如何从 .t​​sv 文件导入数据以在 d3.js 图表中使用?

javascript - 如何从 Meteor/React 的选择选项中获取值?

javascript - 无法为 Javascript 中的对象分配新值

javascript - 尝试让 jQuery 在单击时更改不同的 img src

javascript - 你如何优化网络音频应用程序中的垃圾收集以避免点击噪音?

meteor - Path 适用于桌面设备,但不适用于移动设备

javascript - 有状态组件调用功能组件的 Props 更改类型

html - 将年份值限制为只有 4 位数字