Express API returning an unwanted “data” section in my GET all requests











up vote
0
down vote

favorite












I'm currently building a rest API and I'm having an unexpected output when I make a /GET request.



When i make a get request to the API, it returns



{ 
data: {
[{myExpectedObjects},{myExpectedObjects}]
}
}


however, I'm expecting my get request to return just the array of objects. Below is the code im using to accomplish the rest calls



Create controller



const create = (req, res) => {
let dataModel = generateModel(genericDataFromReq);

dataModel = new dataModel({
genericData,
specificData,
});

dataModel.save().then((data) => {
res.status(201).send(data);
}, (e) => {
res.status(500).send(e);
});
}
};


get all controller



const list = (req, res) => {
const dataModel = generateModel(dataToGet);

dataModel.find().then((data) => {
if (data.length === 0) {
res.status(404).send('failed');
} else {
res.status(200).send({ data });
}
}, (e) => {
res.status(500).send(e);
});
};


generate data model



function generateModel(dbCollectionName) {
try {
return generateDataModel(dbCollectionName);
} catch (e) {
return mongoosee.model(`${dbCollectionName}`);
}
}


I know the code is a bit unconventional but I've set up a generic rest API to take in different types of requests and I found this solution to be the best way of doing this.



Any ideas on why my get all request is tacking on a "data" section before my array of objects (which is what I'm actually interest in)?










share|improve this question


























    up vote
    0
    down vote

    favorite












    I'm currently building a rest API and I'm having an unexpected output when I make a /GET request.



    When i make a get request to the API, it returns



    { 
    data: {
    [{myExpectedObjects},{myExpectedObjects}]
    }
    }


    however, I'm expecting my get request to return just the array of objects. Below is the code im using to accomplish the rest calls



    Create controller



    const create = (req, res) => {
    let dataModel = generateModel(genericDataFromReq);

    dataModel = new dataModel({
    genericData,
    specificData,
    });

    dataModel.save().then((data) => {
    res.status(201).send(data);
    }, (e) => {
    res.status(500).send(e);
    });
    }
    };


    get all controller



    const list = (req, res) => {
    const dataModel = generateModel(dataToGet);

    dataModel.find().then((data) => {
    if (data.length === 0) {
    res.status(404).send('failed');
    } else {
    res.status(200).send({ data });
    }
    }, (e) => {
    res.status(500).send(e);
    });
    };


    generate data model



    function generateModel(dbCollectionName) {
    try {
    return generateDataModel(dbCollectionName);
    } catch (e) {
    return mongoosee.model(`${dbCollectionName}`);
    }
    }


    I know the code is a bit unconventional but I've set up a generic rest API to take in different types of requests and I found this solution to be the best way of doing this.



    Any ideas on why my get all request is tacking on a "data" section before my array of objects (which is what I'm actually interest in)?










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I'm currently building a rest API and I'm having an unexpected output when I make a /GET request.



      When i make a get request to the API, it returns



      { 
      data: {
      [{myExpectedObjects},{myExpectedObjects}]
      }
      }


      however, I'm expecting my get request to return just the array of objects. Below is the code im using to accomplish the rest calls



      Create controller



      const create = (req, res) => {
      let dataModel = generateModel(genericDataFromReq);

      dataModel = new dataModel({
      genericData,
      specificData,
      });

      dataModel.save().then((data) => {
      res.status(201).send(data);
      }, (e) => {
      res.status(500).send(e);
      });
      }
      };


      get all controller



      const list = (req, res) => {
      const dataModel = generateModel(dataToGet);

      dataModel.find().then((data) => {
      if (data.length === 0) {
      res.status(404).send('failed');
      } else {
      res.status(200).send({ data });
      }
      }, (e) => {
      res.status(500).send(e);
      });
      };


      generate data model



      function generateModel(dbCollectionName) {
      try {
      return generateDataModel(dbCollectionName);
      } catch (e) {
      return mongoosee.model(`${dbCollectionName}`);
      }
      }


      I know the code is a bit unconventional but I've set up a generic rest API to take in different types of requests and I found this solution to be the best way of doing this.



      Any ideas on why my get all request is tacking on a "data" section before my array of objects (which is what I'm actually interest in)?










      share|improve this question













      I'm currently building a rest API and I'm having an unexpected output when I make a /GET request.



      When i make a get request to the API, it returns



      { 
      data: {
      [{myExpectedObjects},{myExpectedObjects}]
      }
      }


      however, I'm expecting my get request to return just the array of objects. Below is the code im using to accomplish the rest calls



      Create controller



      const create = (req, res) => {
      let dataModel = generateModel(genericDataFromReq);

      dataModel = new dataModel({
      genericData,
      specificData,
      });

      dataModel.save().then((data) => {
      res.status(201).send(data);
      }, (e) => {
      res.status(500).send(e);
      });
      }
      };


      get all controller



      const list = (req, res) => {
      const dataModel = generateModel(dataToGet);

      dataModel.find().then((data) => {
      if (data.length === 0) {
      res.status(404).send('failed');
      } else {
      res.status(200).send({ data });
      }
      }, (e) => {
      res.status(500).send(e);
      });
      };


      generate data model



      function generateModel(dbCollectionName) {
      try {
      return generateDataModel(dbCollectionName);
      } catch (e) {
      return mongoosee.model(`${dbCollectionName}`);
      }
      }


      I know the code is a bit unconventional but I've set up a generic rest API to take in different types of requests and I found this solution to be the best way of doing this.



      Any ideas on why my get all request is tacking on a "data" section before my array of objects (which is what I'm actually interest in)?







      node.js express






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 11 at 20:54









      Kieran_M

      415




      415
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          I believe the issue is in this line:



          else {
          res.status(200).send({ data });
          }


          When you put curly braces around a variable, it creates a key-value pair where the key is the name of the variable and the value is the value of the variable. So get rid of the curly braces and it should work as you expect. See the parts that mention this ES2015 notation here:



          https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer






          share|improve this answer





















          • You're right. Don't know how I missed that. Thanks
            – Kieran_M
            Nov 11 at 21:08











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53253144%2fexpress-api-returning-an-unwanted-data-section-in-my-get-all-requests%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted










          I believe the issue is in this line:



          else {
          res.status(200).send({ data });
          }


          When you put curly braces around a variable, it creates a key-value pair where the key is the name of the variable and the value is the value of the variable. So get rid of the curly braces and it should work as you expect. See the parts that mention this ES2015 notation here:



          https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer






          share|improve this answer





















          • You're right. Don't know how I missed that. Thanks
            – Kieran_M
            Nov 11 at 21:08















          up vote
          1
          down vote



          accepted










          I believe the issue is in this line:



          else {
          res.status(200).send({ data });
          }


          When you put curly braces around a variable, it creates a key-value pair where the key is the name of the variable and the value is the value of the variable. So get rid of the curly braces and it should work as you expect. See the parts that mention this ES2015 notation here:



          https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer






          share|improve this answer





















          • You're right. Don't know how I missed that. Thanks
            – Kieran_M
            Nov 11 at 21:08













          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          I believe the issue is in this line:



          else {
          res.status(200).send({ data });
          }


          When you put curly braces around a variable, it creates a key-value pair where the key is the name of the variable and the value is the value of the variable. So get rid of the curly braces and it should work as you expect. See the parts that mention this ES2015 notation here:



          https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer






          share|improve this answer












          I believe the issue is in this line:



          else {
          res.status(200).send({ data });
          }


          When you put curly braces around a variable, it creates a key-value pair where the key is the name of the variable and the value is the value of the variable. So get rid of the curly braces and it should work as you expect. See the parts that mention this ES2015 notation here:



          https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 11 at 21:03









          Mixolydian

          963




          963












          • You're right. Don't know how I missed that. Thanks
            – Kieran_M
            Nov 11 at 21:08


















          • You're right. Don't know how I missed that. Thanks
            – Kieran_M
            Nov 11 at 21:08
















          You're right. Don't know how I missed that. Thanks
          – Kieran_M
          Nov 11 at 21:08




          You're right. Don't know how I missed that. Thanks
          – Kieran_M
          Nov 11 at 21:08


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53253144%2fexpress-api-returning-an-unwanted-data-section-in-my-get-all-requests%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Florida Star v. B. J. F.

          Danny Elfman

          Lugert, Oklahoma