【问题标题】:how to retrieve the value from a submitted html form and store it inside the nested document in mongoose schema如何从提交的 html 表单中检索值并将其存储在 mongoose 模式中的嵌套文档中
【发布时间】:2021-03-07 05:04:09
【问题描述】:

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, trim: true, required: true },
    lastName: { type: String, required: true, min: 1, trim: true },
    phoneNumber: { type: String, trim: true, match: [/^\d{3}-\d{3}-\d{4}$/, 'Please Enter Valid Phone Number.'], required: true },
    email: { type: String, trim: true, match: [/^(\w{1,})([\.+-]?\w+)?\@[a-z]{1,}([.-]?\w+){1,}?\.[a-z]{2,6}$/, 'Please Enter Valid Email Address.'] },
    address: [addressSchema] //nested 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: 'luna@yahoo.com',
  address: []
}

这就是我的 req.body

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

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

【问题讨论】:

    标签: html express database-schema mongoose-schema embedded-documents


    【解决方案1】:

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

    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);
        }
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-01
      • 2017-07-20
      • 1970-01-01
      • 2019-03-27
      相关资源
      最近更新 更多