【发布时间】:2020-10-15 20:43:58
【问题描述】:
我已经尝试了很多次更改变量名,甚至从原始源代码复制,但我仍然不明白为什么错误属性显示您声明了错误属性但从未使用过,但在我学习它的视频中工作得很好。我什至无法访问数据库中的数据[为了检查,我使用了console.log 语句,但我仍然没有得到我的数据]。任何人,请帮助我为什么会遇到这个问题。
代码
import React, { useState, useEffect } from 'react';
import Base from '../core/Base';
import { Link } from 'react-router-dom';
import { getCategories } from './helper/adminapicall';
import { isAuthenticated } from '../auth/helper/index';
const AddProduct = () => {
const { user, token } = isAuthenticated();
const [values, setValues] = useState({
name: '',
description: '',
price: '',
stock: '',
photo: '',
categories: [],
category: '',
loading: false,
error: '',
createdProduct: '',
getaRedirect: false,
formData: '',
});
const {
name,
description,
price,
stock,
categories,
category,
loading,
error,
createdProduct,
getaRedirect,
formData,
} = values;
const preload = () => {
getCategories().then((data) => {
console.log(data);
if (data.error) {
setValues({ ...values, error: data.error });
} else {
setValues({ ...values, categories: data, formData: new FormData() });
}
});
};
useEffect(() => {
preload();
}, []);
const createProductForm = () => (
<form>
<span>Post photo</span>
<div className='form-group'>
<label className='btn btn-block btn-success'>
<input
onChange={handleChange('photo')}
type='file'
name='photo'
accept='image'
placeholder='choose a file'
/>
</label>
</div>
<div className='form-group'>
<input
onChange={handleChange('name')}
name='photo'
className='form-control'
placeholder='Name'
value={name}
/>
</div>
<div className='form-group'>
<textarea
onChange={handleChange('description')}
name='photo'
className='form-control'
placeholder='Description'
value={description}
/>
</div>
<div className='form-group'>
<input
onChange={handleChange('price')}
type='number'
className='form-control'
placeholder='Price'
value={price}
/>
</div>
<div className='form-group'>
<select
onChange={handleChange('category')}
className='form-control'
placeholder='Category'>
<option>Select</option>
{categories &&
categories.map((cate, index) => (
<option key={index} value={cate._id}>
{cate.name}
</option>
))}
</select>
</div>
<div className='form-group'>
<input
onChange={handleChange('quantity')}
type='number'
className='form-control'
placeholder='Stock'
value={stock}
/>
</div>
<button
type='submit'
onClick={onSubmit}
className='btn btn-outline-success mb-3'>
Create Product
</button>
</form>
);
return (
<Base
title='Add a product here!'
description='Welcome to product creation section'
className='container bg-info p-4'>
<Link to='/admin/dashboard' className='btn btn-md btn-dark mb-3'>
Admin Home
</Link>
<div className='row bg-dark text-white rounded'>
<div className='col-md-8 offset-md-2'>{createProductForm()}</div>
</div>
</Base>
);
};
export default AddProduct;
【问题讨论】:
-
“出于检查目的,我使用了 console.log 语句,但仍然没有得到我的数据” 所以
data是undefined?这可以解释错误(IMO 非常清楚)。不确定您希望我们做什么。您没有向我们展示getCategories的实现,因此我们无法真正帮助您正确获取数据。 -
“我不明白为什么错误属性显示你声明了错误属性但从未使用过” 这听起来更像是一个 linter 警告,可能指的是你是声明错误 variable/constant 但您没有从中读取(至少在您发布的代码中没有)。不过,这与获取数据无关,是问题中较小的一个。
-
先为数据制作控制台日志,确保你有一个名为data的变量,然后看看你会得到什么,
-
你在
getCategories()里面使用axios吗? -
@CodeBug 我要控制台记录数据未显示,但在我的数据库中,我已经创建了一些数据
标签: javascript reactjs