【问题标题】:Insert form data into mysql database table using node.js and sequelize使用node.js将表单数据插入mysql数据库表并sequelize
【发布时间】:2014-11-07 10:13:58
【问题描述】:

我有一个NodeJS 应用程序,我想使用sequelize()-方法将表单中的一些数据插入到我的MySQL-数据库的表中。

这是我的表格

<form id="addVideo" method="post">
    <input type="url" name="video_url" required></input>
    <input type="hidden" value="" name="artist_id"></input>
    <input type="hidden" value="youtube" name="type"></input>
</form>

我的帖子功能:

$('form#addVideo').submit(function(e){
    e.preventDefault();

    var form = $(this);
    var jsonvideoFormData = utils.serializeToJSON(form);
    var xhrData = _.pick(jsonvideoFormData, 'video_url', 'artist_id', 'type');

    api.post('/videos', xhrData, function(response){
       alert('Video has been added!');
    });
});

那么后端代码是这样的:

exports.addVideo = function(req, res, next){

  var videoURL = req.body.video_url;
  var artistId = req.body.artist_id;
  var type = req.body.type;

  db.sequelize.query('INSERT INTO social_urls (artist_id,urls,type) VALUES('artistId','videoURL','type')', function(err) {
    if(err){
        return res.json(400, {response: {code: 400, message:'An error appeared.'}});
    } else{
       console.log('succes');
       res.json(201, {response: {code: 201, message: 'Video has been added'}});
    }   

  });

}

但由于某种原因,我不知道这不起作用。谁能帮帮我?

非常感谢!!

【问题讨论】:

  • 不工作?你能说得更具体点吗?
  • 我不是 sequelize 方面的专家,但我看到那里的代码容易发生 SQL 注入。

标签: javascript mysql node.js


【解决方案1】:

这是保存数据的实际查询。第 2 步和第 3 步。

  var videoURL = req.body.video_url;
  var artistId = req.body.artist_id;
  var type = req.body.type;

    models.socialUrls.build({ 
            artist_id: artistId, 
            urls: videoURL, 
            type: type
        })
          .save()
          .then(anotherTask => {
            console.log('the data saved!');
            // you can now access the currently saved task with the variable anotherTask... nice!
          })
          .catch(error => {
            console.log('uh oh something wasn't right!');
            console.log(error);
            // Ooops, do some error-handling
          })

如果您在此处查看续集文档: http://docs.sequelizejs.com/manual/tutorial/instances.html

保存数据有 3 个步骤。

  1. 创建模型
  2. 在回调中创建实例,也就是数据对象。这是发送到 sequelize 方法以发送到 db 的内容。
  3. 调用 .save() 方法。

之后,您可以使用 .catch() 处理错误

您的问题似乎出在您的后端代码中。确保您的模型是正确的,并且您的表单中的数据正在发送。确定后,您只需执行第 2 步和第 3 步即可。

【讨论】:

    【解决方案2】:

    我不是 sequelize 方面的专家,但我看到那里的代码容易发生 SQL 注入。

    这是错误的:

    db.sequelize.query('INSERT INTO social_urls (artist_id,urls,type) VALUES('artistId','videoURL','type')', function(err)
    

    至少应该是:

    db.sequelize.query("INSERT INTO social_urls (artist_id,urls,type) VALUES('" + artistId + "','" + videoURL + "','" + type + "')'", function(err)
    

    但实际上,我认为你应该这样做:

    var SocialUrl = sequelize.define('SocialUrl', {
      videoURL: Sequelize.STRING,
      artistId: Sequelize.STRING,
      type:     Sequelize.STRING
    }, {
      tableName: 'social_urls',
      timestamps: false
    });
    
    SocialUrl
      .create({
        videoURL: videoURL,
        artistId: artistId,
        type: type
      })
      .complete(function(err, socialUrl) {
        if (err) {
          // log error;
        } else {
          // Do stuff
        }
      })
    

    【讨论】:

      【解决方案3】:

      您不必对数据进行 JSON 序列化。您可以发布数据。

      <form id="addVideo" method="post" action="/videos">
          <input type="url" name="video_url" required></input>
          <input type="hidden" value="" name="artist_id"></input>
          <input type="hidden" value="youtube" name="type"></input>
      </form>
      

      记住使用body-parser

      app.use(require("body-parser")());
      

      现在 req.body.video_url 应该有预期的数据。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-05-19
        • 2019-01-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-31
        • 1970-01-01
        相关资源
        最近更新 更多