簡體   English   中英

將數組的類型轉換為Mongoose模型

[英]Converting an array's type to Mongoose Model

在nodejs post route中,我將貓鼬模型對象保存到一個數組中,現在我想將該數組保存到mongodb中,為了做到這一點,我需要調用mongoose內置方法array.save()方法。 為此,我需要將此數組轉換為在這種情況下為Form的 Mongoose模型類型。 請告知如何應用此轉化,還是我需要其他解決方案? 貓鼬的模式是:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;    

var formSchema = new Schema({    
    controlType: {type: String, required: true},   
    label: {type: String, required: true},
    required: {type: Boolean}, 
    placeholder: {type: String},   
    options: [String],    //to store options for select or radio input
} ,  {collection: 'inputForm'});

module.exports = mongoose.model('Form', formSchema);

NodeJS發布路線:

router.post('/userform', function (req, res, next) {

    var form = [];    
    for (var key in req.body) {
        if (req.body.hasOwnProperty(key)) {
            var formObj = new Form({
                controlType: req.body[key].controlType,
                label: req.body[key].label,
                required: req.body[key].required,
                placeholder: req.body[key].placeholder,
                options: req.body[key].options
            });                        
        }
        form.push(formObj);        
    }
    console.log('type of form 0528');
    console.log(typeof(form));

    form.save(function(err, result) { // here is the issue, this line is not working because form is not type of mongoose model
        if (err) {
            return res.status(500).json({
                title: 'An error occurred in form api 0528',
                error: err
            });
        }
        res.status(201).json({
            message: 'Form created',
            obj: result
        });
    });    
});

module.exports = router;

Model.create可以采用對象數組來創建多個文檔。

因此,您可以構建對象數組而不是模型實例,然后將該數組傳遞給Form.create

var formArr = []

for (var key in req.body) {

  if (req.body.hasOwnProperty(key)) {

    var formObj = {
      controlType: req.body[key].controlType,
      label: req.body[key].label,
      required: req.body[key].required,
      placeholder: req.body[key].placeholder,
      options: req.body[key].options
    }

    formArr.push(formObj)

  }

}

Form.create(formArr, function(err, results) {

  ...

})



如果要在單個文檔中保存對象數組,請嘗試使用嵌套模式:

var controlSchema = new Schema({    
  controlType: {type: String, required: true},   
  label: {type: String, required: true},
  required: {type: Boolean}, 
  placeholder: {type: String},   
  options: [String]
}, {collection: 'inputForm'})

var formSchema = new Schema({    
  controls: [controlSchema]
})

module.exports = mongoose.model('Form', formSchema)

...然后將數組另存為新文件中的控件字段,如下所示:

var controlsArr = []

for (var key in req.body) {

  if (req.body.hasOwnProperty(key)) {

    var controlObj = {
      controlType: req.body[key].controlType,
      label: req.body[key].label,
      required: req.body[key].required,
      placeholder: req.body[key].placeholder,
      options: req.body[key].options
    }

    controlsArr.push(controlObj)

  }

}

var form = new Form({
  controls: controlsArr
})

form.save(function(err, result) {

  ...

})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM