首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将图像从mongoDB导入react

将图像从mongoDB导入react
EN

Stack Overflow用户
提问于 2017-10-06 20:41:57
回答 1查看 1K关注 0票数 0

我正在尝试在我的mongoDB中植入一个图像源链接,然后将其拉入我的状态,然后尝试加载图像。下面是我的种子和我的反应页面,我将它加载到。

数据库种子:

代码语言:javascript
复制
const houseSeed = [
{
name: "Scenic View",
address: "123 internet st",
imagesrc: "../client/src/images/house5.jpg",
about:
  'This houses biggest selling point is the view from the wall size windows 
put along the entire length off the house.',
date: new Date(Date.now())
},
{
name: "Modern Style",
address: "123 internet st",
imagesrc: "../client/src/images/house4.jpg",
about:
  'This houses biggest selling point is the very modern approach to the 
architecture of the home and the sleek clean lines it contains.',
date: new Date(Date.now())
}

];

react页面,包括整个类、状态和正在呈现的内容。正在尝试将state.source加载到:

代码语言:javascript
复制
class About extends Component {
state = {
    house: {},
    source: ""
};
componentDidMount() {
    API.getHouse(this.props.match.params.id)
        .then(res => this.setState({ house: res.data, source: res.data.imagesrc}))
        .then(console.log("state source" + this.state.source))
        .catch(err => console.log(err));

    console.log("state source" + this.state.source);
}

imageFunct =() => {
    return <img src={this.state.source} alt='image of house' />;
}

render(){
    return(
        <Container fluid>
            <Row>
                <Col size="md-12">
                    <Header />
                    <Nav />
                </Col>
            </Row>
         <Row>
                <Col size="md-3">
                </Col>
                <Col size="md-6">
                    <Row>
                        <Col size="md-12">
                            <h1>{this.state.house.name}</h1>
                        </Col>
                    </Row>
                    <Row>
                        <Col size="md-3" />
                        <Col size="md-6">
                            {this.imageFunct()}
                        </Col>
                        <Col size="md-3" />
                    </Row>
                    <Row>
                        <Col size="md-12">
                            <h4>{this.state.house.about}</h4>
                        </Col>
                    </Row>
                </Col>
                <Col size="md-3">
                </Col>
            </Row>

        </Container>
    )
}

它将显示图像alt,但不显示图像本身。

EN

回答 1

Stack Overflow用户

发布于 2017-10-06 20:46:20

这应该是可行的:

代码语言:javascript
复制
class About extends Component {
state = {
    house: {},
    source: ""
};
componentDidMount() {
    API.getHouse(this.props.match.params.id)
        .then(res => this.setState({ house: res.data, source: res.data.imagesrc}))
        .then(console.log("state source" + this.state.source))
        .catch(err => console.log(err));

    console.log("state source" + this.state.source);
}

render(){
    return(
        <Container fluid>
            <Row>
                <Col size="md-12">
                    <Header />
                    <Nav />
                </Col>
            </Row>
         <Row>
                <Col size="md-3">
                </Col>
                <Col size="md-6">
                    <Row>
                        <Col size="md-12">
                            <h1>{this.state.house.name}</h1>
                        </Col>
                    </Row>
                    <Row>
                        <Col size="md-3" />
                        <Col size="md-6">
                            <img src={this.state.source} alt='image of house' />
                        </Col>
                        <Col size="md-3" />
                    </Row>
                    <Row>
                        <Col size="md-12">
                            <h4>{this.state.house.about}</h4>
                        </Col>
                    </Row>
                </Col>
                <Col size="md-3">
                </Col>
            </Row>

        </Container>
    )
}

但我希望你得到的是完整的网址而不是../client/src/images/house4.jpg?:)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46606008

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档