I am receiving and Invoice Object through req.body. I am assigning the invoiceNumber before saving this object to the database (mongoose). When I assign invoiceNumber to invoicebody.invoiceNumber, it turns to be Undefined but, the result.invoiceNumber = 1.
router.post('/',async(req,res)=>{
try {
let invoicebody = req.body;
await VMaster.findOne({voucherType : 'INV'},{voucherNumber: 1, _id:0})
.then( result =>{
console.log ('First :', result.voucherNumber);
invoicebody.invoiceNumber = result.invoiceNumber;
console.log('Second :', invoicebody.invoiceNumber);
});
const invoice = await Invoice.create(invoicebody);
Vitem.insertMany(invoicebody.itemCollection);
res.status(200).json(invoice);
} catch (error) {
console.log(error.message);
res.status(500).json({message : error.message});
}
})
Console.Log
New contributor
Manickam Parameswaran is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.