I have a stored array of objects within my reactjs application which when console logged looks like this.
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"}
this consists of answer, question and state (state of the answered question not state as in react components)
What I would like to do is pass these through to express so that I can upload them into my SQL database using the npm package mssql. However as these are all stored into the array I'm not sure how to split these up.
Ideally I would love to pass the whole object through to SQL and just store the whole thing in the database e.g (pseudo code)
insert into table where answer = answer and question = question and state = state
Essentially to use this with SQL how would I break these down to be used with my back end or can I pass the whole object with a specific SQL stored procedure.
create procedure StoreAnswers
@{answer_values}
as
INSERT INTO QuestionResponses
(RUId, QuestionId, Date, QuestionWhenAnswered, QuestionResponse, Accepted, AssignedWorkStation )
VALUES
${answer_values}
EDIT
Stored procedure
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;
How it is passed through to the sql query from 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" });
});
This is the console.info(results)
[
[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] ]
error
RequestError: Invalid length parameter passed to the LEFT or SUBSTRING function.