如何通过表单将嵌入代码存储到Mongodb?

时间:2019-04-02 22:50:41

标签: node.js mongodb express

我正在创建一个音乐博客网站,我想通过提交通过表单提交的内嵌框架来嵌入视频。所以我的问题是如何将嵌入代码通过表单存储到Mongodb?

我尝试将视频提交到UTF-8 ECMAScript表单中,但仍然无法正常工作

var artistSchema = new mongoose.Schema({
    name: String,
    title: String,
    image: String,
    content: String,
    description: String
});
// CREATE ROUTE - add new artist to DB
app.post("/songs", function(req, res){
    // get data from form and add to Artist array
    var name = req.body.name;
    var title = req.body.title;
    var image = req.body.image;
    var content = req.body.content;
    var desc = req.body.description;
    var newSong = {name: name, title: title, content: content, image: image, description:desc}
    // Create a new artist and save to DB
    Artist.create(newSong, function(err, newlyCreated){
        if(err){
            console.log(err);
        } else {
            //redirect back to songs page
            res.redirect("/songs");
        }
    });

});

我可以显示所有内容,除了应该是嵌入视频代码的内容

1 个答案:

答案 0 :(得分:0)

这是您的模型:

const artistSchema = new mongoose.Schema({
    name: String,
    title: String,
    image: String,
    content: String,
    description: String
});

这是您的路线。我改写了。

// CREATE ROUTE - add new artist to DB
app.post("/songs", async (req, res) => {
    // get data from form and add to Artist array
    const { name, title, image, content, desc  } = req.body
    const newSong = { name, title, content, image, description:desc }
    try {
      // Create a new artist and save to DB
      await Artist.create(newSong)
      res.redirect('/songs');
    } catch (error) {
      // handle error
      console.log(error)
    }
});

在表格中,您应将desc重命名为description以便进行彻底的销毁。了解您如何发送数据将很有帮助。

<form action="/songs" method="POST">
  <div>
    <input type="text" name="name">
  </div>
  <div>
    <input type="text" name="title">
  </div>
  <div>
    <input type="text" name="content">
  </div>
  <div>
    <input type="text" name="image">
  </div>
  <div>
    <textarea name="desc"></textarea>
  </div>
  <input type="submit" value="Send">
</form>