【发布时间】:2018-09-26 14:32:01
【问题描述】:
我有一个对象以形式传递给 n=modal 组件
data =[
{
_id: "5ba241b4efa8da2f1464ca81",
title: "Zero To One",
author: "Peter Thiel",
isbn: 1279031943,
__v: 0
}
]
我想访问数据上的标题键,但是当我这样做时,我得到了 TypeError。
我尝试使用JSON.stringigy() 将其转换为字符串,但仍然遇到相同的错误。
我正在尝试访问模式中的数据,因此当模式打开时,模式中的输入值是使用表中的数据设置的,因此我可以轻松地编辑它们
当我console.log(data[0]) 时,我得到了对象中的项目,但是当我登录console.log(data[0].title) 时,TypeError 再次出现。
import React, { Component } from 'react';
import './Update.css';
import Search from '../Search/Search';
// import Modal from './Modal/Modal';
const Table = ({ data, openBookDetails }) => (
<table class="table table-hover">
<thead>
<tr class="table-primary">
<th scope="col">Title</th>
<th scope="col">Author</th>
<th scope="col">ISBN</th>
<th scope="col">No. Of Copies</th>
</tr>
</thead>
<tbody>
{data.map(row =>
<TableRow key={row.id} row={row} openBookDetails={openBookDetails}/>
)}
</tbody>
</table>
)
const TableRow = ({ row, openBookDetails }) => (
<tr class="table-light" onClick={openBookDetails}>
<th scope="row" >{row.title}</th>
<td >{row.author}</td>
<td >{row.isbn}</td>
<td >24</td>
</tr>
)
const Modal = ({ closeBookDetails, isBookDetailsOpen, children, data }) => {
const showHideClassName = isBookDetailsOpen ? 'modal display-block' : 'modal display-none';
console.log(data[0].title);
return (
<div className={showHideClassName}>
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Update Book</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="form-group">
<label htmlFor="title">Title</label>
<input type="text" class="form-control" id="title" aria-describedby="title" placeholder="Enter title of book" value={data}/>
</div>
<div class="form-group">
<label htmlFor="author">Author</label>
<input type="text" class="form-control" id="author" aria-describedby="author" placeholder="Enter author name" value={data}/>
</div>
<div class="form-group">
<label htmlFor="isbn">ISBN</label>
<input type="number" min="0" class="form-control" id="isbn" aria-describedby="isbn" placeholder="Enter ISBN number" value={data.title}/>
</div>
<div class="form-group">
<label for="copies">Number of Copies</label>
<input type="number" min="0" class="form-control" id="copies" aria-describedby="copies" placeholder="Enter the number of copies" value={data.title}/>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary">Save changes</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal" onClick={closeBookDetails}>Close</button>
</div>
</div>
</div>
{children}
</div>
);
};
class Update extends Component{
constructor(props) {
super(props);
this.state = {
value: '',
suggestions: [],
setOfAllBooks: [],
searchedBooks: [],
isBookDetailsOpen: false,
};
this.setTableData = this.setTableData.bind(this);
this.openBookDetails = this.openBookDetails.bind(this);
this.closeBookDetails = this.closeBookDetails.bind(this);
}
setTableData(searchedBook){
this.setState({searchedBooks: searchedBook})
console.log(this.state.searchedBooks)
}
openBookDetails(){
console.log('openBookDetails')
this.setState({ isBookDetailsOpen: true})
}
closeBookDetails(){
this.setState({ isBookDetailsOpen: false})
}
render(){
return(
<div>
<Search state={this.state} setTableData={this.setTableData} />
<Table data={this.state.searchedBooks} openBookDetails={this.openBookDetails}/>
<Modal data={this.state.searchedBooks} isBookDetailsOpen={this.state.isBookDetailsOpen} closeBookDetails={this.closeBookDetails} />
{/* <Modal /> */}
</div>
)
}
}
export default Update;
【问题讨论】:
-
你试过这个:
console.log(data[0]['title'])吗? -
刚才试过了。没用
-
这个对象的来源是什么?似乎是一个猫鼬文档,您应该尝试在其上使用 .toObject() 。检查stackoverflow.com/questions/7503450/…
-
还有一件事,您将未定义的值传递给 TableRow 键,因为该对象不包含 id 属性,仅包含私有 _id。但我相信这不是你错误的根源。
-
toObject() 不起作用。
标签: reactjs