1

I want to add data to my MongoDB collection. I'm getting this data via a local Flask API. I'm GETting the data on my React Frontend and it's displaying fine. I'm not sure why I can't do the same thing on my express nodejs backend. I want to get that same data and use it to build the entity that I'm going to store.

This is how I'm attempting to get the data

app.get('/', async (req, res) => {
    let initialData = {};
    axios.get('http://localhost:3000/details').then((res) => {
        initialData = res.data;
      });
    const recruit = new RecruitModel({ email:initialData.email,
                                        mobile_number:initialData.mobile_number,
                                         name:initialData.name});
    try {
        await recruit.save()
        res.send("inserted data")
    } catch (error) {
        console.log(error)
    }
})

I'm pretty sure something wrong there and nowhere else. Because if I pass static information instead it's correctly stored, no issues.

4
  • stackoverflow.com/questions/35228143/… Commented Jul 21, 2022 at 17:52
  • @ISAE This isn't relevant to my question. My object isn't undefined. Commented Jul 21, 2022 at 18:00
  • 1
    But the properties are. Because res.data is only defined inside the promise, and you are trying to access it outside. see how to use promises Commented Jul 21, 2022 at 18:03
  • @ISAE Thanks I got it to work now. If you give those references in an answer I could accept it. Thanks a lot, now I have a better understanding of things Commented Jul 22, 2022 at 10:26

1 Answer 1

1

You are saving to the database's Recruit Collection before the promise is resolved. Since data to save in the Recruit Collection is dependent upon the result from the API which will initially return the promise, therefore, use promise resolving functions to wait for its result.

Solution#1 (using .then function):

app.get('/', async (req, res) => {
  let initialData = {};

  try {
    axios.get('http://localhost:3000/details').then((response) => {
      initialData = response.data;

      const recruit = new RecruitModel({
        email: initialData.email,
        mobile_number: initialData.mobile_number,
        name: initialData.name,
      });

      recruit.save().then((response) => res.send('inserted data'));
    });
  } catch (error) {
    console.log(error);
  }
});

Solution#2 (using async await keywords):

app.get('/', async (req, res) => {
  try {
    const response = await axios.get('http://localhost:3000/details');
    const recruit = new RecruitModel({
      email: response.data.email,
      mobile_number: response.data.mobile_number,
      name: response.data.name,
    });

    await recruit.save();

    res.send('inserted data');
  } catch (error) {
    console.log(error);
  }
});

Either solution will work in your case.

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

3 Comments

I already used the second approach but I'll accept your answer giving that @ISAE didn't post anything
Didn't the second solution work?
It did, @ISAE provided links for how to use promises and I ended up using the second approach, it made more sense to my mind. But he never posted an answer so I'm accepting yours.

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.