0

I am using mongoose for database functionalities in my nodejs project.Below is my model.

Here is the POST request:

enter image description here

In MongoDb data is saving like this :

enter image description here

Here owers array is empty. expense.js

const mongoose = require('mongoose');

const ExpenseSchema = new mongoose.Schema({

  userid:{
      type: String,
      required: true
  },
  owers:[{
     owerid:{
        type: String
      },
     amt:{
       type: Number  
     }
    }],
  name:{
    type: String,
    required: true
  },
  amount:{
      type: Number,
      require: true
  }
});

const expense = mongoose.model('expense',ExpenseSchema);
module.exports = expense;

Whenever I am trying to insert something array is showing empty.Below is my code:

addExpense.js

const expense = require('../models/expense.js');

const addExpense = async (req,res) => {

const {name,amount,owerid,amt} = req.body;
console.log(name + " " + owerid);
const {userid} = req.params;

const expens = new expense({userid,name,amount});

try{
    const data = await expens.save();
    expens.owers.push({"owerid":owerid,"amt":amt});
    res.send({"id":data._id}); 
}
catch(error){
    res.send(error);
}
};

module.exports = {addExpense};

Someone let me know what I am doing wrong.

5
  • Your "addExpense.js" code basically says "get whatever comes in the request and store as a new document". What do you send in the request body, what you expect to be saved in the db? Commented Dec 17, 2021 at 12:00
  • Yeah thats what I want but value of owerid and amt is not storing in array.Everything else is storing. Commented Dec 17, 2021 at 12:04
  • Do you send them as an array? Sorry if my comment was not clear, but the question doesn't make much sense without an example of the data you are trying to save. Commented Dec 17, 2021 at 12:42
  • I have updated my post take a look Commented Dec 17, 2021 at 13:08
  • HTTP is a text based protocol, you know. Postman actually converts this "image" to an http request and sends the json in the request body. The same body that your application recieves in req.body. Try to "Save" the request from Postman and you will see you send a flat object. There is no owers array there. Commented Dec 17, 2021 at 13:15

1 Answer 1

3

Enter owers value like that

Try This
   const {name,amount,owers} = req.body;
        console.log(name + " " + owerid);
        const {userid} = req.params;
        
        const expens = new expense({userid,name,amount});
        
        try{
            const data = await expens.save();
        
           //After you can push multiple data like that
        
          JSON.parse(owers).map((value) => {
            data.owers.push({
                owerid: value.owerid,
                amt: value.amt
            })
        })
        
           data.save()
        
            res.send({"id":data._id}); 
        }
        catch(error){
            res.send(error);
        }
Sign up to request clarification or add additional context in comments.

Comments

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.