node.js - 我正在从我的 nodejs 获取动态数据到 reactjs,但我收到一条错误消息“POST 未定义”
问题描述
我已经使用节点在我的 mongodb 数据库中创建了条目,现在我正在尝试从后端获取该数据以响应前端用于节点中跨平台的第 3 方应用程序是 cors,而反应是 axios(在我添加的脚本中"proxy":"http://localhost:5000"(5000 是我的后端端口)
这是我的 NovelCard.js 代码
import React, { Component } from 'react';
import { Container } from 'react-bootstrap';
import Card from 'react-bootstrap/Card';
import axios from 'axios';
const createSet = (post) => {
<Card style={{ width: '18rem' }}>
<Card.Img variant="top" src="holder.js/100px180" />
<Card.Body>
<Card.Title>{post.name}</Card.Title>
<Card.Subtitle className="mb-2 text-muted">{post.author}</Card.Subtitle>
<Card.Text>{post.synopsis}</Card.Text>
</Card.Body>
</Card>;
};
class Latest extends Component {
state = {
name: '',
author: '',
synopsis: '',
post: [],
};
componentDidMount = () => {
this.getData();
};
getData = () => {
axios
.get('http://localhost:5000/novels/')
.then((res) => {
const data = res.data;
this.setState({ post: data });
console.log('data recived');
})
.catch(() => {
alert('error');
});
};
render() {
console.log('state', this.state);
return <Container>{post.map(createSet)}</Container>;
}
}
export default Latest;
我收到错误说 ***
src\components\home\Latest\NovelCard.js Line 45:24: 'post' is not defined no-undef
解决方案
您的post
变量在您所在的州内可用。render
你需要在你的函数中做这样的事情。
render() {
console.log('state', this.state);
return <Container>{this.state.post.map(createSet)}</Container>;
}
或者你也可以这样做。
render() {
const { post } = this.state;
console.log('state', this.state);
return <Container>{post.map(createSet)}</Container>;
}
推荐阅读
- oracle - Oracle 导出默认日期
- c# - INSERT 语句与我尝试在循环中保存的每个 Second Record 的 FOREIGN KEY 约束冲突
- devops - SaltStack 公式中的手动批准
- c++ - 如何将结构内的数组编组为本机 dll
- log4j - 不同地方的不同日志级别
- python-3.x - 如何在 Windows 7 中自动安装 python 库(在我们的脚本文件中导入)
- dialogflow-es - 保存意图时出现许多弹出错误
- php - 如何获取响应php cURL的“内容长度”
- javascript - ExtJS:如何在函数中更改组件“labelStyle”?
- java - 使用 Apache Tika 将 Docx 文件转换为 Java 中的 Html 时,内容背景和颜色不保留