V
V
Valen962019-05-02 17:25:19
MongoDB
Valen96, 2019-05-02 17:25:19

Why is the body of the request not visible in this POST request?

Tell me, I’m making a POST request to add an item to the body, I pass the text to the body, and on the backend I want to create a body with such text, but when the request body is displayed, it’s empty, why?
const options = {
method: "POST",
body: JSON.stringify(text)
}
fetch(` http://localhost:4000/`, options)
beexports.create = async (req,res,next) => {
console .log("REQ BODY",req.body);
const text = req.body;
try {
const newTodo = new Todo({
text
})
await newTodo.save() console.log(error) } } Router
return res.json(newTodo)
} catch (error) {
const controllers = require('./controllers.js')
router.post('/',controllers.create)

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Anton Spirin, 2019-05-02
@rockon404

To get the request body as res.body in your code, you need to include body-parser :

const express = require('express');
const bodyParser = require('body-parser');

const app = express();

app.use(bodyParser.json());

V
viktorulyushev, 2017-04-05
@viktorulyushev

This script is used for the campaign, but if you remove all the name fields, nothing even helps

; (function (ng) {

    'use strict';

    ng.module('popupwithcallback', []);
})(window.angular);

; (function (ng) {

    'use strict';

    var PopupWithCallbackCtrl = function ($sce, popupwithcallbackService) {

        var ctrl = this, isRender = false;

        ctrl.currentForm = "main";

        ctrl.send = function () {
            //console.log(ctrl);
            popupwithcallbackService.send(ctrl.name, ctrl.phone, ctrl.comment).then(function (result) {
                //console.log(ctrl.name, ctrl.phone, ctrl.comment);
                if (result == true) {
                    ctrl.currentForm = "final";
                    popupwithcallbackService.setVisibleFooter(false);

                    $(document).trigger("module_popupwithcallback");
                    popupwithcallbackService.dialogClose();
                }
            });
        }

        ctrl.dialogOpen = function () {
            //console.log(ctrl);
            if (ctrl.currentForm == "final") {
                ctrl.currentForm = "main";
                ctrl.name = "";
                ctrl.phone = "";
                ctrl.comment = "";
                popupwithcallbackService.setVisibleFooter(true);
            }

            if (isRender) {
                //popupwithcallbackService.dialogOpen();
            } else {
                popupwithcallbackService.getParams().then(function (result) {
                    ctrl.modalText = $sce.trustAsHtml(result.ModalText);
                    ctrl.showCommentField = result.ShowCommentField;
                    //popupwithcallbackService.dialogRender(result.Title, ctrl);
                });
            }
            isRender = true;
        }
    };

    ng.module('popupwithcallback')
      .controller('PopupWithCallbackCtrl', PopupWithCallbackCtrl);

    PopupWithCallbackCtrl.$inject = ['$sce', 'popupwithcallbackService'];

})(window.angular);

; (function (ng) {
    'use strict';

    ng.module('popupwithcallback')
      .directive('popupwithcallbackStart', ['$compile', function ($compile) {
          return {
              restrict: 'A',
              link: function (scope, element, attrs, ctrl) {
                  var popupwithcallbacks = document.querySelectorAll('[data-popupwithcallback]');
                  $compile(popupwithcallbacks)(scope);
              }
          };
      }]);

    ng.module('popupwithcallback')
      .directive('popupwithcallback', function () {
          return {
              restrict: 'A',
              scope: true,
              controller: 'PopupWithCallbackCtrl',
              controllerAs: 'popupwithcallback',
              bindToController: true,
              link: function (scope, element, attrs, ctrl) {
                  element.on('click', function (event) {
                      event.preventDefault();
                      ctrl.dialogOpen();
                      scope.$apply();
                  });
              }
          };
      });
})(window.angular);

; (function (ng) {
    'use strict';

    var popupwithcallbackService = function ($http, modalService) {
        var service = this;

        service.send = function (name, phone, comment) {

            return $http.post('PopupWithCallback/addcallback', { name: name, phone: phone, comment: comment, rnd: Math.random() }).then(function (response) {
                return response.data;
            });
        };

        service.getParams = function () {

            return $http.get('PopupWithCallback/getparams').then(function (response) {
                return response.data;
            });
        };

        service.dialogRender = function (title, parentScope) {

            var options = {
                'modalClass': 'callback-dialog',
                'isOpen': true
            };

            modalService.renderModal(
                'modalCallback',
                title,
                '<div data-ng-include="\'/modules/PopupWithCallback/scripts/templates/modal.html\'"></div>',
                '<input type="submit" class="btn btn-middle btn-action" value="Отправить" data-ng-click="callback.send()" data-ng-disabled="modalCallbackForm.$invalid" />',
                options,
                { callback: parentScope });
        };

        service.dialogOpen = function () {
            modalService.open('popupWithCallback');
        };

        service.dialogClose = function () {
            //modalService.close('popupWithCallback');
            modalService.close('popupWithCallback');
        };

        service.setVisibleFooter = function (visible) {
            modalService.setVisibleFooter('modalCallback', visible);
        };
    };

    ng.module('popupwithcallback')
      .service('popupwithcallbackService', popupwithcallbackService);

    popupwithcallbackService.$inject = ['$http', 'modalService'];

})(window.angular);

T
Taras Kurbatov, 2017-04-05
@TarasKurbatov

I have a question. Where does it open and close?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question