Как отправить результаты опроса JS в базу данных Mongodb с помощью реагировать JS - PullRequest
0 голосов
/ 24 сентября 2019

я создал опрос в ответ, как отправить результат в базу данных mongodb?я новичок, чтобы реагировать

я пытался

import React, { Component } from 'react';


import * as Survey from 'survey-react';
import 'survey-react/survey.css';

import 'bootstrap/dist/css/bootstrap.css'

//import './style.css';

class Hello extends Component {
    componentWillMount() {   

      Survey.Survey.cssType = "bootstrap";

      Survey.defaultBootstrapCss.navigationButton = "btn btn-green";
    }
    render() { 


        var json = {
            "completedHtml": "<h3>Thank you for your feedback.</h3> <h5>Your thoughts and ideas will help us to create a great product!</h5>",
            "completedHtmlOnCondition": [
                {
                    "expression": "{Score_values} > 2",
                    "html": "<h3>Thank you for your feedback.</h3> <h5>We glad that you love our product. Your ideas and suggestions will help us to make our product even better!</h5>"
                }, {
                    "expression": "{Score_values} < 1",
                    "html": "<h3>Thank you for your feedback.</h3> <h5> We are glad that you share with us your ideas.We highly value all suggestions from our customers. We do our best to improve the product and reach your expectation.</h5>\n"
                }
            ],
            "pages": [
                {

                 "name": "page2",
                 "elements": [
                  {

                   "type": "rating",
                   "name": "Score_values",
                   "title": " How is your experience after shopping with us?",
                   "isRequired": true,
                   "rateMax": 10,
                   "minRateDescription": "Unhappy",
                   "maxRateDescription": " Delighted"
                  },
                  {
                   "type": "checkbox",
                   "name": "ques1",
                   "visibleIf": "{Score_values} <= 4",
                   "title": "What are the issues you faced?",
                   "choices": [
                    "Availability of product of your choice",
                    "Ease of locating the product",
                    "Store layout",
                    "Staff Behaviour",
                    "Other (describe)"
                   ]
                  },
                  {
                   "type": "comment",
                   "name": "comm1",
                   "visibleIf": "{Score_values} <= 4",
                   "title": "More on Issue faced"
                  },
                  {
                   "type": "comment",
                   "name": "comm2",
                   "visibleIf": "{Score_values} <= 4",
                   "title": "Your Mobile number"
                  }
                 ]
                }
               ],
               "showQuestionNumbers": "off"
              };
              function sendDataToServer(survey) {
                alert("The results are:" + JSON.stringify(survey.data) + ". The results can be sent to a API server and save to a database.");

                var data = {
                  "request": "save_data_1",
                  sdata: survey.data
                };

                $.ajax({
                  headers: {},
                  type: "POST",
                  url: "127.0.0.1:27017/research.biomarker",
                  contentType: "application/json",
                  charset: "utf-8",
                  dataType: "json",
                  error: function(jqXHR, error, errorThrown) {
                    if (jqXHR.status) {
                      alert(jqXHR.responseText);
                    } else {
                      alert("Something went wrong");
                    }
                  },
                  data: JSON.stringify(data),
                  success: function(c, textStatus, request) {
                  },
                })
              }


      var model = new Survey.Model(json);
      $("surveyResult").Survey({
        model: model,
        onComplete: sendDataToServer
      }) 

      return (
        <Survey.Survey model={model}/>

      );

    }

  }
export default Hello;

И это функция внутри компонента для отправки данных на сервер.

function sendDataToServer(survey) {
  alert("The results are:" + JSON.stringify(survey.data) + ". The results can be sent to a API server and save to a database.");

  var data = {
    "request": "save_data_1",
    sdata: survey.data
  };

  $.ajax({
    headers: {},
    type: "POST",
    url: "127.0.0.1:27017/research.biomarker",
    contentType: "application/json",
    charset: "utf-8",
    dataType: "json",
    error: function(jqXHR, error, errorThrown) {
      if (jqXHR.status) {
        alert(jqXHR.responseText);
      } else {
        alert("Something went wrong");
      }
    },
    data: JSON.stringify(data),
    success: function(c, textStatus, request) {
    },
  })
}
...