[英]Error when trying to create fake data with faker.js
At first, I install faker using the command npm i faker首先,我使用命令npm i faker 安装 faker
Now i am trying to run my npm server up现在我正在尝试运行我的 npm 服务器
npm run start. npm 运行启动。
But i am getting the following error.但我收到以下错误。 Can some one please help me here and thanks in advance.有人可以在这里帮助我并提前致谢。
**Compiled with problems:X **编译时出现问题:X
ERROR in./src/context/Context.js 5:0-26 /src/context/Context.js 5:0-26 中的错误
Module not found: Error: Can't resolve 'faker' in shoppingcartapplication\src\context**未找到模块:错误:无法解析 shoppingcartapplication\src\context 中的“faker”**
import React, { createContext } from 'react';
import faker from "faker";
const Cart = createContext();
faker.seed(20);
const Context = ({ children }) => {
const products = [...Array(20)].map(() => ({
id: faker.datatype.uuid(),
name: faker.commerce.productName(),
price: faker.commerce.price(),
image: faker.random.image(),
inStock: faker.random.arrayElement([0, 3, 5, 6, 7]),
fastDelivery: faker.datatype.boolean(),
ratings: faker.random.arrayElement([1, 2, 3, 4, 5]),
}));
console.log(products);
return (
(
<Cart.Provider >
{children}
</Cart.Provider>
)
)
}
export default Context;
faker is deprecated first remove your old package不推荐使用faker 首先删除您的旧package
npm uninstall faker
then get a new package然后得到一个新的 package
npm install @faker-js/faker --save-dev
and import it like this并像这样导入它
import * as faker from 'faker';
import * as faker from '@faker-js/faker';
npm install --save-dev faker@5.5.3
this will solve the issue这将解决问题
import { faker } from '@faker-js/faker';
//single random user profile generator
function randomProfile() {
return {
userId: faker.datatype.uuid(),
username: faker.internet.userName(),
email: faker.internet.email(),
avatar: faker.image.avatar(),
password: faker.internet.password(),
birthdate: faker.date.birthdate(),
registeredAt: faker.date.past(),
}
}
//define a method to generate users up to 'max_size' amount
const profile = function (max_size) {
const users = [];
for (let index = 0; index < max_size; index++) {
users.push(randomProfile());
}
return users;
};
// actually generate 10 random user profiles & load them in 'users_group' variable
const users_group = profile(10);
const Home = () => {
return (
<View style={{flex: 1, backgroundColor: '#fff'}}>
<StatusBar hidden/>
{/* out of the 10 user profile generated use the avatar(Image) of the first user*/}
<Image source={users_group[1].avatar} />
{/* out of the 10 user profile generated use the second avatar(Image) of the first user*/}
<Image source={users_group[2].avatar} />
<Text>Hello{profile.avatar}</Text>
</View>
)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.