2017-09-06 8 views
2

Mongooseで.save()を使用するとDBが正しく更新されないため、私のMongooseスキーマが間違っているはずです。以下のnodeJSルートが実行された後、コールバックで受け取った更新されたオブジェクトIは正しいですが、それ以降の実行では、DBオブジェクトは空で更新されませんでした。ここで私のマングーススキーマは間違っていますか?

は私のデータがどのように見えるかの例です。今ここに

 var newApplicationData = new ApplicationData({ 
     pos1: 0, 
     pos2: 0, 
     applicationData: [ 

      // Standard One 
      { 
       sections: [ 
        { 
         narrative: '', 
         documentation: [] 
        }, 
        { 
         narrative: '', 
         documentation: [] 
        }, 
        { 
         narrative: '', 
         documentation: [] 
        } 
       ] 
      }, 

      // Standard 2 
      { 
       sections: [ 
        { 
         narrative: '', 
         documentation: [] 
        }, 
        { 
         narrative: '', 
         documentation: [] 
        }, 
        { 
         narrative: '', 
         documentation: [] 
        }, 
        { 
         narrative: '', 
         documentation: [] 
        } 
       ] 
      }, 

      // Standard 3 
      { 
       sections: [ 
        { 
         narrative: '', 
         documentation: [] 
        }, 
        { 
         narrative: '', 
         documentation: [] 
        }, 
        { 
         narrative: '', 
         documentation: [] 
        }, 
        { 
         narrative: '', 
         documentation: [] 
        }, 
        { 
         narrative: '', 
         documentation: [] 
        }, 
        { 
         narrative: '', 
         documentation: [] 
        }, 
        { 
         narrative: '', 
         documentation: [] 
        } 
       ] 
      }, 

      // Standard 4 
      { 
       sections: [ 
        { 
         narrative: '', 
         documentation: [] 
        }, 
        { 
         narrative: '', 
         documentation: [] 
        }, 
        { 
         narrative: '', 
         documentation: [] 
        }, 
        { 
         narrative: '', 
         documentation: [] 
        }, 
        { 
         narrative: '', 
         documentation: [] 
        }, 
        { 
         narrative: '', 
         documentation: [] 
        }, 
        { 
         narrative: '', 
         documentation: [] 
        }, 
        { 
         narrative: '', 
         documentation: [] 
        }, 
        { 
         narrative: '', 
         documentation: [] 
        } 
       ] 
      }, 
     ] 
    }); 

私のマングースのスキーマです:

const mongoose = require('mongoose'); 

const ApplicationDataSchema = mongoose.Schema({ 
pos1: { 
    type: Number, 
    required: true, 
}, 
pos2: { 
    type: Number, 
    required: true, 
}, 
applicationData: { 
    type: Array, 
    required: true, 

    sections: { 
     type: Array, 
     required: true, 

     narrative: { 
      type: String, 
      required: true 
     }, 
     documentation: { 
      type: Array, 
      required: true 
     } 
    } 
} 
}, { collection : 'applicationData' }); 

const ApplicationData = module.exports = mongoose.model('ApplicationData', ApplicationDataSchema); 

そして最後に、ここで私がしようとしている私のNodeJSルートがあります.SAVEを使用してデータを保存():

app.post('/upload', multer({ storage: storage }).single('file'), (req, res) => { 

    if(!req.file) return res.send({ status: 400, msg: 'No File Received.' }); 

    let pos1 = req.body.pos1; 
    let pos2 = req.body.pos2; 

    ApplicationData.findById(mongoose.Types.ObjectId(req.body.applicationDataID), (err, applicationDataObject) => { 

     if(err) return res.send({ status: 400, msg: err }); 

     applicationDataObject.applicationData[pos1].sections[pos2].documentation.push(req.file.filename); 

     applicationDataObject.save((err, updatedApplicationData) => { 

     console.log(applicationDataObject.applicationData[pos1].sections[pos2].documentation); 

     if(err) return res.send({ status: 400, msg: err }); 

     return res.send({ status: 200, applicationData: updatedApplicationData.applicationData }); 
     }); 
    }); 
}); 

答えて

2

あなたがarray of objectを使用している、を置きますtype

 { 
      pos1: { 
      type: Number, 
      required: true, 
      }, 
      pos2: { 
      type: Number, 
      required: true, 
      }, 
      applicationData: { 
      type: [{ 
       sections: { 
       type: [{ 
        narrative: { 
        type: String, 
        required: true, 
        }, 
        documentation: { 
        type: Array, 
        required: true, 
        }, 
       }], 
       required: true, 
       }, 
      }], 
      required: true, 
      }, 
     } 

ベストのように、サブスキーマカントーを使用することですへ:

const subSchema2 = new mongoose.Schema({ 
    narrative: { 
    type: String, 
    required: true, 
    }, 
    documentation: { 
    type: Array, 
    required: true, 
    }, 
}) 

const subSchema1 = new mongoose.Schema({ 
    sections: { 
    type: [subSchema2], 
    required: true, 
    }, 
}) 

const finalSchema = new mongoose.Schema({ 
    pos1: { 
    type: Number, 
    required: true, 
    }, 
    pos2: { 
    type: Number, 
    required: true, 
    }, 
    applicationData: { 
    type: [subSchema1], 
    required: true, 
    }, 
}) 
+1

ありがとうございました!!!!!! – rgins16

関連する問題