В моем приложении reactjs хранится массив объектов, который при входе в консоль выглядит следующим образом.
0: {answer: "yes↵", question: "Is the keyboard separate from the screen?", state: "Declined with soloution defined"}
1: {answer: "yes", question: "Does the keyboard tilt?", state: "Accepted"}
2: {answer: "Question declined But not a problem", question: "Is it possible to find a comfortable typing postion?", state: "Accepted"}
3: {answer: "yes", question: "Do you have a good keyboard technique?", state: "Accepted"}
4: {answer: "Question declined But not a problem", question: "Are the characters on the keyboard clear and readable?", state: "Accepted"}
5: {answer: "sgdfgdfgdf", question: "Is your mouse or other pointing device suitable to the task you're using it for?", state: "problem specified"}
6: {answer: "yes", question: "Is the mouse (or other pointing device) located sufficently close to you? ", state: "Accepted"}
7: {answer: "sdfsdfsdfsd", question: "Is there support for your wrist and forearm when using the mouse(or other pointing device)", state: "Declined with soloution defined"}
8: {answer: "yes", question: "Does the mouse (or other pointing device) work smoothly at a speed that suits you?", state: "Accepted"}
9: {answer: "asdasdas", question: "Can you easily adjust the software settings for speed and accuracy of the pointer?", state: "Declined with soloution defined"}
10: {answer: "asdasdads", question: "Are the characters on your screen clear and readable?", state: "problem specified"}
11: {answer: "yes", question: "Is the text size on your screen confortable to read?", state: "Accepted"}
12: {answer: "asdasdasd", question: "Is the image on your screen free from flicker and jitter?", state: "Declined with soloution defined"}
13: {answer: "asdasdasd", question: "Is your screen's specification suitable for its intended use?", state: "problem specified"}
14: {answer: "yes", question: "Is the brightness and/or contrast on your screen adjustable?", state: "Accepted"}
это состоит из ответа, вопроса и состояния (состояние отвеченного вопроса, а не состояние, как в компонентах реагирования)
Я хотел бы передать их на express чтобы я мог загрузить их в свою базу данных SQL, используя пакет npm ms sql. Однако, поскольку они все хранятся в массиве, я не уверен, как их разделить.
В идеале я хотел бы передать весь объект в SQL и просто сохранить все это в базе данных, например (псевдокод)
insert into table where answer = answer and question = question and state = state
По существу, использовать это с SQL как бы я разбил их для использования с моим бэкэндом или я могу передать весь объект с помощью указанной c SQL хранимой процедуры.
create procedure StoreAnswers
@{answer_values}
as
INSERT INTO QuestionResponses
(RUId, QuestionId, Date, QuestionWhenAnswered, QuestionResponse, Accepted, AssignedWorkStation )
VALUES
${answer_values}
РЕДАКТИРОВАТЬ
Сохранено процедура
create procedure StoreAnswers(@Results varchar(max))
as begin
insert into QuestionResponses(QuestionResponse, QuestionWhenAnswered, State)
select substring(value, charindex('{answer: "', value) + 10, charindex('", ', value, charindex('{answer: "', value) + 10) - charindex('{answer: "', value) - 10) as answer,
substring(value, charindex('question: "', value) + 11, charindex('", ', value, charindex('question: "', value) + 11) - charindex('question: "', value) - 11) as question,
substring(value, charindex('state: "', value) + 8, charindex('"}', value, charindex('state: "', value) + 8) - charindex('state: "', value) - 8) as state
from string_split(@Results, char(10))
end;
Как она передается в запрос sql из express npm
app.post("/post-question-answers", async (req, res) => {
console.log("!called");
let results = req.body.results;
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);
});
});
res.statusCode = 400;
res.statusMessage = "bad request";
// res.json({ message: "Email Missing" });
});
Это console.info (результаты)
[
[0] {
[0] answer: 'yes',
[0] question: 'Is the keyboard separate from the screen?',
[0] state: 'Accepted'
[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: 'yes',
[0] question: 'Is the mouse (or other pointing device) located sufficently close to 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: '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: '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] ]
ошибка
RequestError: Invalid length parameter passed to the LEFT or SUBSTRING function.