gpt4 book ai didi

javascript - 为什么 "this.props.match.params.authorName"在 ReactJS 中给出未定义/空值

转载 作者:行者123 更新时间:2023-11-30 19:31:14 26 4
gpt4 key购买 nike

我正在尝试获取发送的查询参数 authorName,因此我可以获得该值并搜索 MongoDB

我正在通过另一个名为 Authors 的组件调用 FilterBooks 组件

这是我的Authors.jsx

import React, {Component} from 'react';
import axios from 'axios';
import {BrowserRouter as Router, Route, Link} from 'react-router-dom'
import FilterBooks from './filter-books'

const SelectOption = (props) => (
<option value={props.author.firstName}>{`${props.author.firstName} ${props.author.lastName}`}</option>
)

export default class Authors extends Component{

constructor(props){
super(props);
this.state = {
authorArray: [],
authorName: ''
}
this.onChange = this.onChange.bind(this);
}

componentDidMount(){
axios.get('http://localhost:4000/books/authors')
.then(res => {
this.setState({
authorArray: res.data
});
});
}


getAuthors(){
return this.state.authorArray.map((currentAuthor, id) => {
return <SelectOption author={currentAuthor} key={id} />
});
}

onChange(e){
this.setState({
authorName: e.target.value
});

}


render(){
return(
<Router>
<div className="container">
<h3>Authors List</h3>

<div className="row">
<div className="col-md-12 text-center">
<select
value={this.state.authorName}
onChange={this.onChange}
className="form-control" id="authors" name="authors">
<option>- Author List -</option>
{this.getAuthors()}
</select>
<br />
{/* <Link to={`/authors/${this.state.authorName}`} className="btn btn-success" >View Books</Link> */}
<Link to={`/authors/${this.state.authorName}`} className="btn btn-success" >View Books</Link>
</div>
</div>

</div>
{/* <Route path={`/authors/${this.state.authorName}`} exact Component={FilterBooks} /> */}
<Route path={`/authors/${this.state.authorName}`} component={FilterBooks}/>
</Router>
);
}
}

这是我的FilterBook.jsx

import React, {Component} from 'react'
import axios from 'axios';

export default class FilterBooks extends Component{

constructor(props){
super(props);
}

componentDidMount(){
axios.get("http://localhost:4000/books/authors/" + this.props.match.params.authorName)
.then(res => {
console.log(res.data);
}).catch(err => {
console.log(err);
});
}

render(){
return(
<div>
<h1>Hello Books</h1>
</div>
);
}
}

这是我的 server.js,它处理后端。在这里,我删除了其他端点,因此这段代码看起来简洁明了。

const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const mongoose = require('mongoose');

const BookDB = require('./book-dbmodel');
const AuthorDB = require('./author-dbmodel');

const app = express();
const router = express.Router();

app.use(bodyParser.json());
app.use(cors());
app.use('/books', router);

//connect to books database
mongoose.connect('mongodb://127.0.0.1:27017/books', {useNewUrlParser: true});

const connection = mongoose.connection;
connection.once('open', () => {
console.log("Connected to MongoDB via port 27017");
});

app.listen(4000, () => {
console.log('Listening to port 4000');
});


//get books by author name http://localhost:4000/books/authors/authorName
router.route('/authors/:authorName').get((req, res) => {
let authorName = req.params.authorName;
BookDB.find({firstName: {$regex: `${authorName}`, $options: "i"}}, (err, books) => {
if(err) throw err;
res.status(200).send(books);
});
});

但这是我的输出,它是一个空输出:

output

有人可以帮帮我吗?

最佳答案

要使 URL 路径的一部分成为参数,您应该这样定义它

<Route path='/authors/:authorName' component={FilterBooks}/>

然后你可以在你的组件中访问它

this.props.match.params.authorName

关于javascript - 为什么 "this.props.match.params.authorName"在 ReactJS 中给出未定义/空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56416789/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com