首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尝试使用Marak/faker.js导入数据时出错

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

Stack Overflow用户
提问于 2016-09-02 04:03:55
回答 1查看 3.5K关注 0票数 4

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

我的导入文件:

代码语言:javascript
复制
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');
			    }
			});
		});
	}
});

这是我用来写注释的方法文件:

代码语言:javascript
复制
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,
});

这是我在终端中得到的错误代码: TypeError: Cannot read property 'lorem‘of undefined。

任何帮助都是非常感谢的。

编辑:

按照建议,我将导入从"import { lorem,faker } from ' faker ';“更改为"import faker from 'faker';”

我还将这个"faker.lorem.title();“更改为"faker.hacker.noun();”

谢谢Guig!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-09-03 04:44:22

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

代码语言:javascript
复制
import faker from 'faker';
// then use `faker.lorem` as you are currently doing

代码语言:javascript
复制
import { lorem } from 'faker';
// then use `lorem` instead of `faker.lorem`

目前,您正在执行以下操作

代码语言:javascript
复制
import { lorem, faker } from 'faker';

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

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39280020

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档