0

I have the following Schema (with some options taken out for simplicity):

const SubmitDebtSchema = new Schema ({
  balance: [{
    balanceDate: Date,
    newBalance: Number
  }]
});

And I have the following serverless function which posts the Schema to my database:

module.exports = async (req, res) => {

  res.statusCode = 200;
  res.setHeader("Content-Type", "application/json");

  await SubmitDebt.create(req.body)
  .then(() => {
    res.send(JSON.stringify({ success: true }));
  })
  .catch((err) => {
    console.log(err);
    res.send(JSON.stringify({ success: false }));
  });

};

To post to my database, I have the following Axios post call:

  onSubmit = async (e) => {

    e.preventDefault();

    const balanceDate = new Date();

    await axios.post("/api/submit/submitDebt", {
      balanceDate: balanceDate,
      newBalance: this.state.balance,
    })

    this.props.history.push('/dashboard');
  }

However, every time I submit the request, it just returns an empty array. The 'balance' array gets put into my database, but there's never anything in it. It just saves like this:

Empty MongoDB array

Can anyone point out where I'm going wrong here, and why my database entries aren't saving in my array?

It's worth pointing out - the rest of the call works fine! The entry is saved, it's just the array which is the problem.

6
  • Should the balance hold an object or an array of objects? Commented Nov 23, 2020 at 8:26
  • An array of objects. :-) Commented Nov 23, 2020 at 8:27
  • okay, what is SubmitDebt? Commented Nov 23, 2020 at 8:28
  • also when should a new object be added to the balance array and when should a new item be added to the collection? Commented Nov 23, 2020 at 8:31
  • SubmitDebt is how the SubmitDebtSchema is imported in to my serverless function file. And at the moment, this function is just designed to create a new item for the collection. A new balance array will be done via update afterwards in a separate function. Commented Nov 23, 2020 at 8:36

1 Answer 1

1

Try modifying the function this way to match your schema:

module.exports = async (req, res) => {

  res.statusCode = 200;
  res.setHeader("Content-Type", "application/json");
  const submitDebt = {
   balance: [{...req.body}] // it might be better to first extract what you need and use it here.
  };

  await SubmitDebt.create(submitDebt)
  .then(() => {
    res.send(JSON.stringify({ success: true }));
  })
  .catch((err) => {
    console.log(err);
    res.send(JSON.stringify({ success: false }));
  });

};

I also noticed that you're using both await and .then, either one of them is enough.

module.exports = async (req, res) => {
    
      res.statusCode = 200;
      res.setHeader("Content-Type", "application/json");
      
      const { balanceDate, newBalance } = req.body;
      const submitDebt = {
       balance: [{balanceDate, newBalance}]
      };
      try {
       await SubmitDebt.create(submitDebt)
       res.send(JSON.stringify({ success: true }));
      } catch(err) {
       console.log(err);
       res.send(JSON.stringify({ success: false }));
      }
    };
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the response. I've got it working now by adjusting the Axios post to reflect the object. So balance -> newBalance, balanceDate. But this would also work too, so accepting! Thanks for the help.
@JonNicholson Great! also note that you don't need both await and .then see my second snippet.

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.