1

I am running Reactjs and mongdb. In Reactjs, once user selects the value, I am sending that into mongob

addTodo(event) {
        event.preventDefault();
        var roomID = this.state.selectedRoomID;
         console.log(RoomID); //This value is printed correctly

        const { mongodb } = this.props;
        mongodb.db("attendance")
            .collection("MasterDB").find({"RoomID": roomID}).asArray()
            .then(MasterDB => {
                this.setState({ MasterDB }); console.log("inside"+this.state.MasterDB.length);
            });
        console.log("outside"+this.state.MasterDB.length)

a) Here first console.log is printing correctly. b) when ever I do find with roomID=1 explicitly the Query method is working. whenever I keep roomID=this.state.selectedRoomID;. Query is not working because roomID is not getting any value. I am not sure why it is not getting that value. c) When I execute I see, console.log(outside) is printed first and than console.logg(inside) is printed.

As this is sequential, I am expecting the order other way. is there a way I can make sure first the lookup in mongodb happens first that is (console.log(inside) is printed first followed by console.log(outside)?

I see this as issue with reactjs the way I am sending the variables

3
  • I feel this issue is the way the commads are getting executed, is there a way in reactjs I can make it sequential Commented Dec 30, 2019 at 6:18
  • you may want to check that you don't have roomID declared as a global variable somewhere else in your file(s), even if only initialized. Commented Dec 30, 2019 at 6:34
  • Using mongodb directly inside React app is not secure. How will you hide your mongodb credentials? Commented Dec 30, 2019 at 8:01

1 Answer 1

4

I need to look at the code when selectedRoomID is set to figure out why it is not getting any value.

As for the code to execute synchronously, either use Promises or Async/Await. That should help you run the code in the way you want it.

For example, in the code you posted, if you put the console log statement inside the then function, it should work fine. Or you can make the whole mongodb command a promise and wait for it to finish before proceeding further.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for reply. This is the code. I can see in console RoomID getting printed from a variable it reads from state? is there anything else
I can see that the console statement has RoomID as the variable, while the one being used in the mongodb command is roomID. Those are two different variables. Do you have a global RoomID anywhere in your code? Also try printing the state variable as it is. See if it is what you expect it to be.
Thank you for the response. Yes RooMID s the field in DB. roomID is the variable which I am assinging with the value user selected that I received from state.selectedID

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.