尝试使用Marak/faker.js导入数据时出错
Error when trying to import data with Marak/faker.js
我似乎找不到我的问题。有人知道我做错了什么吗?这个项目是用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,
});
这是我在我的终端中得到的错误代码:TypeError:无法读取未定义的属性lorem .
非常感谢任何帮助。
编辑:按照建议,我将import从"import {lorem, faker} from 'faker';"更改为"import faker from 'faker';"
我还将这个"faker. loremm .title();"更改为"faker.hacker.noun();"
谢谢Guig !
看起来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.
错误。
相关文章:
- 如何将.json文件作为参数导入.js文件并使用命令行启动它
- 在哪里放置JavaScript警报覆盖,以便所有页面都可以使用它,而无需导入*.js
- 在不使用外部库的情况下本机导入.js文件
- 如何在nodejs中导入js文件(在cloud9中)
- Typescript 1.8 - ES2015 导入 js 文件
- 导入.js会禁用jQuery Mobile CSS样式
- 使用Tumblr API时如何在QML中导入JS文件
- 导入.js脚本时,单击时没有响应
- 404在nodejs+Ionic应用程序中从index.html中的另一个文件夹导入js文件时出错
- 在XSLT中导入JS文件和Javascript
- 带有FlowType的React Native:导出和导入js接口
- 加载/导入js代码的需求或其他东西
- 从哪里导入js文件更好?
- Ember Brocfile不导入JS和CSS文件
- 为什么我导入js文件的地方会影响我的调用
- 如何在Angular 2项目中导入js包
- 如果不需要或不导入js库,则公开它的最佳方式
- 如何在Ionic2项目中导入js文件和引用
- 当iFrame导入JS库时,浏览器是否会再次导入该库?
- JScript:导入js文件