无法使用Angular将使用NodeJS的数组内的嵌套数组发布到MongoDB

时间:2020-05-26 16:24:24

标签: node.js mongodb express mongoose angular8

这是我的Angular TS文件:

   const salaryInfo = {
    payrollDetails: ( this.payrollDetails)
  } 

  console.log('salaryInfo :', this.payrollDetails );

  this.payrollSalaryService.newSalary(salaryInfo).subscribe(data => {
    if (!data.success) {
      this.messageClass = 'alert alert-danger';
      this.message = data.message; 
      this.processingTransaction = false; 
    } else {
      this.messageClass = 'alert alert-success'; 
      this.message = data.message; 
      this.performAuthentication()
      setTimeout(() => {
        this.processingTransaction = false; 
        this.message = false;
        jQuery("#payrollModal").modal("hide");
      }, 1000);
    }
  }); 
从数据库中查询

this.payrollDetails。 从浏览器查看时,它返回一个名称和一个嵌套数组:work []

我的猫鼬模式是这样的:

const mongoose = require('mongoose'); 
mongoose.Promise = global.Promise; 
const Schema = mongoose.Schema; 

const payrollSalarySchema = new Schema({
    date: {type: String },
    payrollDetails: [{
        branch: { type: String },
        department: { type: String },
        lastName: { type: String },
        name: { type: String },
        work: [{
            regularWorkHours: { type: Number },
          //  hourlyRate: { type: String },
        //    holidayCredit: { type: String },
        }]
      }]
});

module.exports = mongoose.model('PayrollSalary', payrollSalarySchema);

我在NodeJS中的路由文件是这样的:

router.post('/newSalary', async (req, res) => {

    console.log('payrollDetails :', req.body.payrollDetails);


    try {
        const payrollSalary = new PayrollSalary({
            date: new Date(),
            payrollDetails: req.body.payrollDetails,
        });
        await payrollSalary.save();
        res.json({ success: true, message: 'Salary Processing Completed.' });
    } catch (err) { res.json({ success: false, message: 'An error occured' }); }
});


module.exports = router;

当我console.log('payrollDetails:',req.body.payrollDetails);时在nodejs中,嵌套数组work []为空。但是,这不是空的,就像在浏览器中登录控制台一样。

此外,如果未从数据库中查询this.payrollDetails并将其替换为常量值,则会出现嵌套数组work []。

0 个答案:

没有答案