代码之家  ›  专栏  ›  技术社区  ›  imin

将objectID列为select中的值并保存表单

  •  1
  • imin  · 技术社区  · 7 年前

    我有两个模式,一个是学校,另一个是老师。下面是模式的样子

    var SchoolSchema = new Schema({    
        name: {
            type: String,
            required: true,        
            unique: true
        },
        status: {
            type: Number,
            default: 1
        }
    });
    
    module.exports = mongoose.model('School', SchoolSchema);
    

    老师:

    var TeacherSchema = new Schema({    
        schoolID: {
            type: mongoose.Schema.Types.ObjectId, ref: 'School'
        },
        name: {
            type: String,
            required: true
        },
        email: {
            type: String,
            unique: true,
            required: true
        },
    });
    module.exports = mongoose.model('Teacher', TeacherSchema );
    

    <b-select id="schoolID" name="schoolID">
                    <option selected>Select the school</option>
                    <option v-for="school in schools" v-bind:value="school._id" v-bind:key="school._id">{{ school.name }}</option>
                </b-select>
    

    以下是报名老师表格中的全文部分:

    <script>
    
        import axios from 'axios'
    
        export default {
            name: 'register',
            data () {
                return {
                    input: {
                        name: "",
                        email: "",
                        password: "",
                        schoolID: ""
                    },
                    schools: [],
                    result : "",
                    errors: []
                }
            },
            created() {
                axios.get('http://localhost:8081/public/school/list')
                .then(response => {
                    this.schools = response.data
                })
                .catch(e => {
                    this.errors.push(e)
                })
            },
    
            methods: {
                onSubmit (evt) {
                    evt.preventDefault()
                    if(this.input.name != "" && this.input.email != "" && this.input.password != "") {
    
                        axios.post('http://localhost:8081/api/auth/teacher/register', this.input)
                        .then(response => {
                            if (response.data.success == true){                            
                               console.log("teacheradded!")
                               this.result = "teacheraddded!"
                            }else{
                                console.log(response.data.msg)
                                this.errors.push(response.data.msg)
                            }
                        })
                        .catch(e => {
                            console.log(e.response)
                            this.errors.push(e)
                        })
    
                    }
                }
            }
        }
    </script>
    

    这是教师/注册下的部分代码

    router.post('/teacher/register', function(req, res) {
      if (!req.body.name || !req.body.email || !req.body.password) {
        res.json({success: false, msg: 'Please enter your name, email and password.'});
      } else {
        var newTeacher = new Teacher({
          schoolID: req.body.schoolID,
          name: req.body.name,
          email: req.body.email,
          password: req.body.password
        });
        // save the teacher
        newTeacher .save(function(err) {
          if (err) {
            console.log(err)
            return res.json({success: false, msg: 'Error'});
          }
          res.json({success: true, msg: 'Successfully created new teacher.'});
        });
      }
    });
    

    但我似乎无法得到学校的objectID,它被指定为select中的值。

    1 回复  |  直到 7 年前
        1
  •  1
  •   Boussadjra Brahim    7 年前

    你得把你的衣服绑起来 b-select 组件到您的 input.schoolID v-model="input.schoolID"

      <b-select id="schoolID" name="schoolID" v-model="input.schoolID">
                <option selected>Select the school</option>
                <option v-for="school in schools" v-bind:value="school._id" v-bind:key="school._id">{{ school.name }}</option>
      </b-select>