我如何将一组对象传递到我的 express/sql 服务器(反应)

Posted

技术标签:

【中文标题】我如何将一组对象传递到我的 express/sql 服务器(反应)【英文标题】:How could I pass an array of objects through to my express/sql server (react) 【发布时间】:2020-02-18 13:16:26 【问题描述】:

您好,我有一个对象数组,我想将它们传递到我的后端(sql server)。

下面是对象数组,这是来自

的确切结果
 console.info(results)

这里是对象数组(命名为结果)

[0] [
[0]   
[0]     answer: 'bmbm,nn',
[0]     question: 'Is the keyboard separate from the screen?',
[0]     state: 'problem specified'
[0]   ,
[0]   
[0]     answer: 'yes',
[0]     question: 'Does the keyboard tilt?',
[0]     state: 'Accepted'
[0]   ,
[0]   
[0]     answer: 'yes',
[0]     question: 'Is it possible to find a comfortable typing postion?',
[0]     state: 'Accepted'
[0]   ,
[0]   
[0]     answer: 'yes',
[0]     question: 'Do you have a good keyboard technique?',
[0]     state: 'Accepted'
[0]   ,
[0]   
[0]     answer: 'yes',
[0]     question: 'Are the characters on the keyboard clear and readable?',
[0]     state: 'Accepted'
[0]   ,
[0]   
[0]     answer: 'yes',
[0]     question: "Is your mouse or other pointing device suitable to the task you're using it for?",
[0]     state: 'Accepted'
[0]   ,
[0]   
[0]     answer: 'bnmgjvgh',
[0]     question: 'Is the mouse (or other pointing device) located sufficently close to you?  ',
[0]     state: 'Declined with soloution defined'
[0]   ,
[0]   
[0]     answer: 'yes',
[0]     question: 'Does the mouse (or other pointing device) work smoothly at a speed that suits you?',
[0]     state: 'Accepted'
[0]   ,
[0]   
[0]     answer: 'yes',
[0]     question: 'Is there support for your wrist and forearm when using the mouse(or other pointing device)',
[0]     state: 'Accepted'
[0]   ,
[0]   
[0]     answer: 'yes',
[0]     question: 'Can you easily adjust the software settings for speed and accuracy of the pointer?',
[0]     state: 'Accepted'
[0]   ,
[0]   
[0]     answer: 'yes',
[0]     question: 'Are the characters on your screen clear and readable?',
[0]     state: 'Accepted'
[0]   ,
[0]   
[0]     answer: 'yes',
[0]     question: 'Is the text size on your screen confortable to read?',
[0]     state: 'Accepted'
[0]   ,
[0]   
[0]     answer: 'yes',
[0]     question: 'Is the image on your screen free from flicker and jitter?',
[0]     state: 'Accepted'
[0]   ,
[0]   
[0]     answer: 'yes',
[0]     question: "Is your screen's specification suitable for its intended use?",
[0]     state: 'Accepted'
[0]   ,
[0]   
[0]     answer: 'yes',
[0]     question: 'Is the brightness and/or contrast on your screen adjustable?',
[0]     state: 'Accepted'
[0]   
[0] ]

这就是我最初将它传递给我的 sql 的方式,但是我坚持如何这样做的逻辑。

app.post("/post-question-answers", async (req, res) => 
  console.log("!called");
  try 
    let results = req.body.results;

//This is the console.info posted earlier 

    console.info(results);

    await sql.connect(config, function(err) 
      if (err) console.log(err);

      // create Request object
      var request = new sql.Request();
      request.input("Results", sql.VarChar, results);
      // query to the database and get the records
      request.execute("dbo.StoreAnswers", function(err, recordset) 
        if (err) console.log(err);
        // send records as a response
        res.json(recordset);
      );
    );
   catch (e) 
    console.info(e);
  
  res.statusCode = 400;
  res.statusMessage = "bad request";
  // res.json( message: "Email Missing" );
);

我只需要知道这是如何实现的。我是否需要遍历并删除每个单独的变量?我需要在 sql 中使用用户定义的表吗?我可以将对象直接传递到数据库中吗?

我不知道接下来会发生什么,并且一直在这个问题上浪费时间。

请多多指教!!!

【问题讨论】:

我认为您的问题不清楚,需要编辑。 什么不清楚,如果需要我很乐意更新 :) 【参考方案1】:

您可以使用原始查询批量插入

【讨论】:

这种形式的答案应该在评论中。 好的,谢谢 有什么地方可以查看这些示例吗?

以上是关于我如何将一组对象传递到我的 express/sql 服务器(反应)的主要内容,如果未能解决你的问题,请参考以下文章

如何将一组数据从客户端传递到 axios post 请求的 req.body

如何将一组 RelationalGroupedDataset 传递给函数?

如何将一组 java 对象表示为数据库中其他对象的字段?

通过子程序将一组变量值传递给没有公共块的函数都有哪些方法?

如何将一组选中/未选中的复选框值传递给 PHP 电子邮件生成器?

如何从swift将一组浮点数组传递给C++函数