如何从提交的 html 表单中检索值并将其存储在 mongoose 模式中的嵌套文档中

问题描述

猫鼬架构:

const addressSchema = new Schema({
    street: { type: String,trim: true },state: { type: String,trim: true,match: [/^[a-zA-Z]{2,}/,'Please Enter State in a Correct Format']},zip: {type: Number,match:[/^[0-9]{5}([-][0-9]{3})?$/,'Please Enter Valid Zip Code']}
});

/*
 * Contact Schema
 */
const contactSchema = new Schema({
    firstName: { type: String,required: true },lastName: { type: String,required: true,min: 1,phoneNumber: { type: String,match: [/^\d{3}-\d{3}-\d{4}$/,'Please Enter Valid Phone Number.'],email: { type: String,match: [/^(\w{1,})([\.+-]?\w+)?\@[a-z]{1,}([.-]?\w+){1,}?\.[a-z]{2,6}$/,'Please Enter Valid Email Address.'] },address: [addressSchema] //@R_404_6431@ted instead of a reference
},{ timestamps: true }
);
module.exports = mongoose.model('Contact',contactSchema);

使用 EJS 的 HTML 表单:

<form action="/contacts/new" method="POST">
    <div>
        <label for="first-name">First Name:
        <input type="text" name="firstName">
      </div>

      <div>
        <label for="last-name">Last Name:
        <input type="text" id="last-name" name="lastName">
        </label>
      </div>

      <div>
        <label for="phone"> Phone: 
        <input type="text" id="phone" name="phoneNumber" placeholder="111-111-111">
        </label>
      </div>

      <div>
        <label for="email"> Email: 
        <input type="email" id="email" name="email">
        </label>
      </div>
      <br/>
      <p> Address: </p>
      <br />
      <div>
        <label for="street-address"> Street: 
        <input type="text" id="street-address" name="street">
        </label>
      </div>

      <div>
        <label for="state-address"> State: 
        <input type="text" id="state-address" name="state">
        </label>
      </div>

      <div>
        <label for="zip-address"> Zip Code: 
        <input type="number" id="zip-address" name="zip">
        </label>
      </div>
</form>

我的控制器如下:

module.exports.addNewContact = async (req,res,next) => {
    try {
        let newContact = new ContactModel(req.body);
        await newContact.save();
        res.redirect('/contacts');
    } catch (err) {
        console.log(err);
    }
};

这是 get 存储在数据库中的内容

{
  _id: 6044570c86de7d07d93e1be5,firstName: 'Luna',lastName: 'hh',phoneNumber: '123-123-1232',email: '[email protected]',address: []
}

这是我的req.body

[Object: null prototype] {
  firstName: 'Tetet',lastName: 'Tic',email: '',street: 'King AS',state: '',zip: '12333'
}

我的问题是,您如何从表单中检索所有密钥并将其存储在 addressSchema 中? 我想在实例化 ContactModel 对象时添加 req.body 会获取所有值并将其添加到相应的架构中。

解决方法

所以基本上我所要做的就是向对象推送额外的地址。

module.exports.addNewContact = async (req,res,next) => {
    try {
        let newContact = new ContactModel(req.body);

        //since addressSchema field is empty,I had to push the address field again
        newContact.address.push(req.body);

        await newContact.save();
        res.redirect('/contacts');
    } catch (err) {
        console.log(err);
    }
};