简体   繁体   English

如何使用 mongoose、nodejs 从 Mongodb 获取数据

[英]How can i get data from Mongodb using mongoose, nodejs

I'm just started learning Nodejs, mongodb, reactjs and having a trouble.我刚开始学习 Nodejs、mongodb、reactjs 并且遇到了麻烦。

I'm trying to get data from mongodb.我正在尝试从 mongodb 获取数据。

backend code:后台代码:

app.post('/tweet',(req,res)=>{
    tweetData.find((err,data)=>{
        if(err){
            return res.json({success: false, error: err});
        }
        return res.json({success: true, data:data});
    });
})

font end code:字体结束代码:

class TweetContainer extends React.Component{

    constructor(props){
        super(props);
        this.state = {
            data:null,
            API_URL : 'http://localhost:5000/tweet'
        }
    }

    componentDidMount(){

        fetch(this.state.API_URL)
        .then(response=>response.json)
        .then(result=>{
            this.setState({
                data: result.data
            });
            console.log(this.state.data)
        });

    }
    render(){

        return(
            <div id="main">
                <h2>Tweet</h2>
                <div id="stream">

                </div>
            </div>
        );
    }
}

when i log the data, console show undefined.当我记录数据时,控制台显示未定义。

What i'm doing wrong.我做错了什么。

Edit 1:编辑1:

You're also querying with mongoose, wrong as it takes the first argument as the query:您也在使用 mongoose 进行查询,这是错误的,因为它将第一个参数作为查询:

app.post('/tweet',(req,res)=>{
    tweetData.find(queryObject || {}, (err,data)=>{
        if(err){
            return res.json({success: false, error: err});
        }
        return res.json({success: true, data:data});
    });
})

You're logging the state immediately after you're updating it, let me tell you that's not how it works, it doesn't immediately update the state, so you have to wait for it to do.您在更新状态后立即记录状态,让我告诉您这不是它的工作原理,它不会立即更新状态,因此您必须等待它完成。

You can pass a callback to setState , so that it executes the callback after the state change has been completed, something like this:您可以将回调传递给setState ,以便它在状态更改完成后执行回调,如下所示:

this.setState({ data: result.data }, () => {
    console.log(this.state)
})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 nodejs 和 mongoose 从具有动态嵌套级别的 mongodb 获取数据 - Get data from mongodb with dynamic nested levels using nodejs and mongoose 如何使用猫鼬从 MongoDb 获取数据? - How to get Data from MongoDb using mongoose? 如何使用nodejs和mongoose从mongodb集合中删除数据 - How to remove data from mongodb collection using nodejs and mongoose 如何使用 mongoose 和 nodejs 从 mongodb 获取与每条记录匹配的所有数据 - How to get all data matched each record from mongodb using mongoose and nodejs 如何连接两个表(文档)并使用猫鼬(express,nodejs)获取不匹配的数据? - How can i join two tables(documents) and get unmatched data using mongoose(express,nodejs)? NodeJs猫鼬如何从“ find()。then”中的“ find()。then”中获取数据? - NodeJs Mongoose How can I get out a data from “find().then” in a “find().then”? 如何从节点 express mongoose mongoDb 中的地理位置数据中获取城市名称 - How can I get city name from geolocation data in node express mongoose mongoDb 如何让在 docker 下运行的 nodejs 服务器通过 mongoose 连接到 mongodb? - How can I get a nodejs server running under docker get connected by mongoose to mongodb? 如何使用 mongoose 从 mongodb 获取两个日期之间的数据 - How to get data between the two dates from mongodb using mongoose 使用 nodejs 和 mongoose 从 mongodb 中具有 id 的对象数组中获取元素 - Get an element from an array of objects with an id in mongodb using nodejs and mongoose
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM