GET request with different mongo find results on same page The Next CEO of Stack OverflowNode.js: Not able to return Mongoose find results“Large data” work flows using pandasmongo $in query isn't working with sails.js find methodSequelizeConnectionRefusedError json mysqlWhy this error coming while running nodejs server?express-ejs-layout sometimes fails to detect passing argumentnode-mongo-native and mongo-shell return different results given the same queryExpress req.body not WorkingI got an empty array in sub document array saving using mongoose ( MEAN stack)Sails How to redirect page when bad request/400 error occurs?

Are the names of these months realistic?

Physiological effects of huge anime eyes

IC has pull-down resistors on SMBus lines?

Help/tips for a first time writer?

What would be the main consequences for a country leaving the WTO?

Traduction de « Life is a roller coaster »

Defamation due to breach of confidentiality

How to get the last not-null value in an ordered column of a huge table?

Does destroying a Lich's phylactery destroy the soul within it?

It is correct to match light sources with the same color temperature?

Decide between Polyglossia and Babel for LuaLaTeX in 2019

What steps are necessary to read a Modern SSD in Medieval Europe?

Help understanding this unsettling image of Titan, Epimetheus, and Saturn's rings?

"Eavesdropping" vs "Listen in on"

Redefining symbol midway through a document

Does higher Oxidation/ reduction potential translate to higher energy storage in battery?

Is it okay to majorly distort historical facts while writing a fiction story?

Computationally populating tables with probability data

Inexact numbers as keys in Association?

What CSS properties can the br tag have?

Graph of the history of databases

What is the process for purifying your home if you believe it may have been previously used for pagan worship?

what's the use of '% to gdp' type of variables?

My ex-girlfriend uses my Apple ID to login to her iPad, do I have to give her my Apple ID password to reset it?



GET request with different mongo find results on same page



The Next CEO of Stack OverflowNode.js: Not able to return Mongoose find results“Large data” work flows using pandasmongo $in query isn't working with sails.js find methodSequelizeConnectionRefusedError json mysqlWhy this error coming while running nodejs server?express-ejs-layout sometimes fails to detect passing argumentnode-mongo-native and mongo-shell return different results given the same queryExpress req.body not WorkingI got an empty array in sub document array saving using mongoose ( MEAN stack)Sails How to redirect page when bad request/400 error occurs?










0















I would like my website to have a search bar in the top section that returns a single document (ink) from a mongo database. On the same page, I would like to be able to access all documents from the same database.



I'm having trouble trying to figure out how to do this on one page, since I can only send one result to URL.



Is there some way to send all documents to the page, then do a search with AJAX on the client side? I'm new to coding, and wondering if I'm going about this wrong.



I appreciate any help. Here is part of my code that sends the results I want, but to different pages.



app.get("/", function(req, res)

// FIND ONE INK FROM DB
var noMatch = null;
if(req.query.search)
Ink.find(ink: req.query.search, function(err, foundInk)
if(err)
console.log(err);
else
if(foundInk.length < 1)
noMatch = "No match, please try again.";

res.render('new-index', ink: foundInk, locationArray: locationArray, noMatch: noMatch )

);
else

// FIND ALL INKS FROM DB
Ink.find(, function(err, allInks)
if(err)
console.log(err);
else
res.render("index", ink: allInks, locationArray: locationArray, noMatch: noMatch );

);

);









share|improve this question


























    0















    I would like my website to have a search bar in the top section that returns a single document (ink) from a mongo database. On the same page, I would like to be able to access all documents from the same database.



    I'm having trouble trying to figure out how to do this on one page, since I can only send one result to URL.



    Is there some way to send all documents to the page, then do a search with AJAX on the client side? I'm new to coding, and wondering if I'm going about this wrong.



    I appreciate any help. Here is part of my code that sends the results I want, but to different pages.



    app.get("/", function(req, res)

    // FIND ONE INK FROM DB
    var noMatch = null;
    if(req.query.search)
    Ink.find(ink: req.query.search, function(err, foundInk)
    if(err)
    console.log(err);
    else
    if(foundInk.length < 1)
    noMatch = "No match, please try again.";

    res.render('new-index', ink: foundInk, locationArray: locationArray, noMatch: noMatch )

    );
    else

    // FIND ALL INKS FROM DB
    Ink.find(, function(err, allInks)
    if(err)
    console.log(err);
    else
    res.render("index", ink: allInks, locationArray: locationArray, noMatch: noMatch );

    );

    );









    share|improve this question
























      0












      0








      0








      I would like my website to have a search bar in the top section that returns a single document (ink) from a mongo database. On the same page, I would like to be able to access all documents from the same database.



      I'm having trouble trying to figure out how to do this on one page, since I can only send one result to URL.



      Is there some way to send all documents to the page, then do a search with AJAX on the client side? I'm new to coding, and wondering if I'm going about this wrong.



      I appreciate any help. Here is part of my code that sends the results I want, but to different pages.



      app.get("/", function(req, res)

      // FIND ONE INK FROM DB
      var noMatch = null;
      if(req.query.search)
      Ink.find(ink: req.query.search, function(err, foundInk)
      if(err)
      console.log(err);
      else
      if(foundInk.length < 1)
      noMatch = "No match, please try again.";

      res.render('new-index', ink: foundInk, locationArray: locationArray, noMatch: noMatch )

      );
      else

      // FIND ALL INKS FROM DB
      Ink.find(, function(err, allInks)
      if(err)
      console.log(err);
      else
      res.render("index", ink: allInks, locationArray: locationArray, noMatch: noMatch );

      );

      );









      share|improve this question














      I would like my website to have a search bar in the top section that returns a single document (ink) from a mongo database. On the same page, I would like to be able to access all documents from the same database.



      I'm having trouble trying to figure out how to do this on one page, since I can only send one result to URL.



      Is there some way to send all documents to the page, then do a search with AJAX on the client side? I'm new to coding, and wondering if I'm going about this wrong.



      I appreciate any help. Here is part of my code that sends the results I want, but to different pages.



      app.get("/", function(req, res)

      // FIND ONE INK FROM DB
      var noMatch = null;
      if(req.query.search)
      Ink.find(ink: req.query.search, function(err, foundInk)
      if(err)
      console.log(err);
      else
      if(foundInk.length < 1)
      noMatch = "No match, please try again.";

      res.render('new-index', ink: foundInk, locationArray: locationArray, noMatch: noMatch )

      );
      else

      // FIND ALL INKS FROM DB
      Ink.find(, function(err, allInks)
      if(err)
      console.log(err);
      else
      res.render("index", ink: allInks, locationArray: locationArray, noMatch: noMatch );

      );

      );






      node.js mongodb express mongoose






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 8 at 17:29









      nchilesnchiles

      34




      34






















          2 Answers
          2






          active

          oldest

          votes


















          0














          You can use separated endpoints for each request. For the full access request, you can render the page, calling res.render, and for the search request, you can return a json calling res.json. Something like this:



          app.get("/", function(req, res)
          // FIND ALL INKS FROM DB
          Ink.find(, function(err, allInks)
          if(err)
          console.log(err);
          else
          res.render("index", ink: allInks, locationArray: locationArray, noMatch: noMatch )

          );
          )

          app.get("/search", function(req, res)
          // FIND ONE INK FROM DB
          var noMatch = null;
          Ink.findOne(ink: req.query.search, function(err, foundInk)
          if(err)
          console.log(err);
          else
          if(!foundInk)
          noMatch = "No match, please try again.";


          res.json(ink: foundInk, locationArray: locationArray, noMatch: noMatch )

          );
          });


          Note the call to Ink.findOne in the /search handler, which will return only one document.



          This way you can make and AJAX request to /search, and parse the json returned from the server.



          I've created a sample repository with the exact same issue here






          share|improve this answer

























          • I made an AJAX request in my main.js by clicking search button in EJS file: $(".search-button").click(function() $.ajax( url: '/search', type: 'GET', dataType: "json", success: function(data) console.log(data); ); ); but it's logging null. If I do a get request straight from the form to /search, it returns my search result object. I know you said parse the json, but I'm not sure where to do that, or if that's why it's returning null from the AJAX. Thanks again for your help.

            – nchiles
            Mar 9 at 4:40











          • Try adding an error function to your AJAX call to print the error and send the result here. Also, in your main.js, check how are the parameters coming from the AJAX request and the browser request, to see if we are missing something.

            – Renato Vassão
            Mar 9 at 13:20











          • I added the error function, but I'm not getting an error back. To check how the parameters are coming from AJAX and browser I tried this, to make sure the query was being sent to the AJAX and am getting an alert now with the query: $(".search-button").click(function() *query = $('#autocompleteInks').val()* $.ajax( url: '/search' + '?search=' + query, type: 'GET', dataType: "json", success: function(data) alert(data.ink.ink); , error: function(err) console.log(err); ); ); but I can't use <%=ink.ink %> or in EJS

            – nchiles
            Mar 9 at 14:55











          • Ok, so if the success function is executing it means that your server is returning a response with status 2xx. Try checking the request and response on the server side. Investigate their properties like req.query and the foundInk object you are returning. Also, try making the request to your server via another client, such as cURL or postman, to see what exactly is happening.

            – Renato Vassão
            Mar 9 at 15:09











          • Ok, in postman I can do a GET to (localhost:3000/search?search=133) (or whatever number at the end) and get an object of the query returned, so there must be something wrong with my AJAX? If I do url: '/search' + '?search=' + query, type: 'GET', dataType: "json", success: function(data) $('.result').html(data.ink.ink); alert(data); , 133 will briefly show in my EJS and disappear, but the alert is "[object Object]"

            – nchiles
            Mar 9 at 15:36


















          0














          Ideally you make an endpoint like this.



          ( id parameter is optional here...thats why the '?' ) 
          www.example.com/api/inks/:id?


          // return all the inks
          www.example.com/api/inks

          // return a specific ink with id=2
          www.example.com/api/inks/2


          So now you can render all the links via /inks and search a particular ink by using the endpoint /ink/:id?



          Hope this helps !






          share|improve this answer























          • Thank you for your help. Right now my endpoint for all inks is www.example.com/ and when I search, the endpoint is www.example.com/?search=123. I'm confused on the part where I'm rendering the page and sending ink: foundInk or ink: allInks. Is there a way to use those as variable and send both with the endpoint www.example.com/2? Sorry if I'm not making sense.

            – nchiles
            Mar 8 at 18:31











          • Yeaah...I am not getting what you're trying to say. Use what as a variable and send what and why to the endpoint /2 ?

            – Hemant Parashar
            Mar 8 at 18:51











          • I was using /2 from your example as the endpoint. And I was wondering if there was a way to use the result from the search as a variable, to use on the client side. Basically, I don't understand how (at any endpoint), there can be results from Ink.find() and Ink.find(ink: req.query.search). Would it help to show my ejs file?

            – nchiles
            Mar 8 at 19:37











          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',
          autoActivateHeartbeat: false,
          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%2f55068182%2fget-request-with-different-mongo-find-results-on-same-page%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          You can use separated endpoints for each request. For the full access request, you can render the page, calling res.render, and for the search request, you can return a json calling res.json. Something like this:



          app.get("/", function(req, res)
          // FIND ALL INKS FROM DB
          Ink.find(, function(err, allInks)
          if(err)
          console.log(err);
          else
          res.render("index", ink: allInks, locationArray: locationArray, noMatch: noMatch )

          );
          )

          app.get("/search", function(req, res)
          // FIND ONE INK FROM DB
          var noMatch = null;
          Ink.findOne(ink: req.query.search, function(err, foundInk)
          if(err)
          console.log(err);
          else
          if(!foundInk)
          noMatch = "No match, please try again.";


          res.json(ink: foundInk, locationArray: locationArray, noMatch: noMatch )

          );
          });


          Note the call to Ink.findOne in the /search handler, which will return only one document.



          This way you can make and AJAX request to /search, and parse the json returned from the server.



          I've created a sample repository with the exact same issue here






          share|improve this answer

























          • I made an AJAX request in my main.js by clicking search button in EJS file: $(".search-button").click(function() $.ajax( url: '/search', type: 'GET', dataType: "json", success: function(data) console.log(data); ); ); but it's logging null. If I do a get request straight from the form to /search, it returns my search result object. I know you said parse the json, but I'm not sure where to do that, or if that's why it's returning null from the AJAX. Thanks again for your help.

            – nchiles
            Mar 9 at 4:40











          • Try adding an error function to your AJAX call to print the error and send the result here. Also, in your main.js, check how are the parameters coming from the AJAX request and the browser request, to see if we are missing something.

            – Renato Vassão
            Mar 9 at 13:20











          • I added the error function, but I'm not getting an error back. To check how the parameters are coming from AJAX and browser I tried this, to make sure the query was being sent to the AJAX and am getting an alert now with the query: $(".search-button").click(function() *query = $('#autocompleteInks').val()* $.ajax( url: '/search' + '?search=' + query, type: 'GET', dataType: "json", success: function(data) alert(data.ink.ink); , error: function(err) console.log(err); ); ); but I can't use <%=ink.ink %> or in EJS

            – nchiles
            Mar 9 at 14:55











          • Ok, so if the success function is executing it means that your server is returning a response with status 2xx. Try checking the request and response on the server side. Investigate their properties like req.query and the foundInk object you are returning. Also, try making the request to your server via another client, such as cURL or postman, to see what exactly is happening.

            – Renato Vassão
            Mar 9 at 15:09











          • Ok, in postman I can do a GET to (localhost:3000/search?search=133) (or whatever number at the end) and get an object of the query returned, so there must be something wrong with my AJAX? If I do url: '/search' + '?search=' + query, type: 'GET', dataType: "json", success: function(data) $('.result').html(data.ink.ink); alert(data); , 133 will briefly show in my EJS and disappear, but the alert is "[object Object]"

            – nchiles
            Mar 9 at 15:36















          0














          You can use separated endpoints for each request. For the full access request, you can render the page, calling res.render, and for the search request, you can return a json calling res.json. Something like this:



          app.get("/", function(req, res)
          // FIND ALL INKS FROM DB
          Ink.find(, function(err, allInks)
          if(err)
          console.log(err);
          else
          res.render("index", ink: allInks, locationArray: locationArray, noMatch: noMatch )

          );
          )

          app.get("/search", function(req, res)
          // FIND ONE INK FROM DB
          var noMatch = null;
          Ink.findOne(ink: req.query.search, function(err, foundInk)
          if(err)
          console.log(err);
          else
          if(!foundInk)
          noMatch = "No match, please try again.";


          res.json(ink: foundInk, locationArray: locationArray, noMatch: noMatch )

          );
          });


          Note the call to Ink.findOne in the /search handler, which will return only one document.



          This way you can make and AJAX request to /search, and parse the json returned from the server.



          I've created a sample repository with the exact same issue here






          share|improve this answer

























          • I made an AJAX request in my main.js by clicking search button in EJS file: $(".search-button").click(function() $.ajax( url: '/search', type: 'GET', dataType: "json", success: function(data) console.log(data); ); ); but it's logging null. If I do a get request straight from the form to /search, it returns my search result object. I know you said parse the json, but I'm not sure where to do that, or if that's why it's returning null from the AJAX. Thanks again for your help.

            – nchiles
            Mar 9 at 4:40











          • Try adding an error function to your AJAX call to print the error and send the result here. Also, in your main.js, check how are the parameters coming from the AJAX request and the browser request, to see if we are missing something.

            – Renato Vassão
            Mar 9 at 13:20











          • I added the error function, but I'm not getting an error back. To check how the parameters are coming from AJAX and browser I tried this, to make sure the query was being sent to the AJAX and am getting an alert now with the query: $(".search-button").click(function() *query = $('#autocompleteInks').val()* $.ajax( url: '/search' + '?search=' + query, type: 'GET', dataType: "json", success: function(data) alert(data.ink.ink); , error: function(err) console.log(err); ); ); but I can't use <%=ink.ink %> or in EJS

            – nchiles
            Mar 9 at 14:55











          • Ok, so if the success function is executing it means that your server is returning a response with status 2xx. Try checking the request and response on the server side. Investigate their properties like req.query and the foundInk object you are returning. Also, try making the request to your server via another client, such as cURL or postman, to see what exactly is happening.

            – Renato Vassão
            Mar 9 at 15:09











          • Ok, in postman I can do a GET to (localhost:3000/search?search=133) (or whatever number at the end) and get an object of the query returned, so there must be something wrong with my AJAX? If I do url: '/search' + '?search=' + query, type: 'GET', dataType: "json", success: function(data) $('.result').html(data.ink.ink); alert(data); , 133 will briefly show in my EJS and disappear, but the alert is "[object Object]"

            – nchiles
            Mar 9 at 15:36













          0












          0








          0







          You can use separated endpoints for each request. For the full access request, you can render the page, calling res.render, and for the search request, you can return a json calling res.json. Something like this:



          app.get("/", function(req, res)
          // FIND ALL INKS FROM DB
          Ink.find(, function(err, allInks)
          if(err)
          console.log(err);
          else
          res.render("index", ink: allInks, locationArray: locationArray, noMatch: noMatch )

          );
          )

          app.get("/search", function(req, res)
          // FIND ONE INK FROM DB
          var noMatch = null;
          Ink.findOne(ink: req.query.search, function(err, foundInk)
          if(err)
          console.log(err);
          else
          if(!foundInk)
          noMatch = "No match, please try again.";


          res.json(ink: foundInk, locationArray: locationArray, noMatch: noMatch )

          );
          });


          Note the call to Ink.findOne in the /search handler, which will return only one document.



          This way you can make and AJAX request to /search, and parse the json returned from the server.



          I've created a sample repository with the exact same issue here






          share|improve this answer















          You can use separated endpoints for each request. For the full access request, you can render the page, calling res.render, and for the search request, you can return a json calling res.json. Something like this:



          app.get("/", function(req, res)
          // FIND ALL INKS FROM DB
          Ink.find(, function(err, allInks)
          if(err)
          console.log(err);
          else
          res.render("index", ink: allInks, locationArray: locationArray, noMatch: noMatch )

          );
          )

          app.get("/search", function(req, res)
          // FIND ONE INK FROM DB
          var noMatch = null;
          Ink.findOne(ink: req.query.search, function(err, foundInk)
          if(err)
          console.log(err);
          else
          if(!foundInk)
          noMatch = "No match, please try again.";


          res.json(ink: foundInk, locationArray: locationArray, noMatch: noMatch )

          );
          });


          Note the call to Ink.findOne in the /search handler, which will return only one document.



          This way you can make and AJAX request to /search, and parse the json returned from the server.



          I've created a sample repository with the exact same issue here







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 18 at 19:59

























          answered Mar 8 at 22:33









          Renato VassãoRenato Vassão

          915




          915












          • I made an AJAX request in my main.js by clicking search button in EJS file: $(".search-button").click(function() $.ajax( url: '/search', type: 'GET', dataType: "json", success: function(data) console.log(data); ); ); but it's logging null. If I do a get request straight from the form to /search, it returns my search result object. I know you said parse the json, but I'm not sure where to do that, or if that's why it's returning null from the AJAX. Thanks again for your help.

            – nchiles
            Mar 9 at 4:40











          • Try adding an error function to your AJAX call to print the error and send the result here. Also, in your main.js, check how are the parameters coming from the AJAX request and the browser request, to see if we are missing something.

            – Renato Vassão
            Mar 9 at 13:20











          • I added the error function, but I'm not getting an error back. To check how the parameters are coming from AJAX and browser I tried this, to make sure the query was being sent to the AJAX and am getting an alert now with the query: $(".search-button").click(function() *query = $('#autocompleteInks').val()* $.ajax( url: '/search' + '?search=' + query, type: 'GET', dataType: "json", success: function(data) alert(data.ink.ink); , error: function(err) console.log(err); ); ); but I can't use <%=ink.ink %> or in EJS

            – nchiles
            Mar 9 at 14:55











          • Ok, so if the success function is executing it means that your server is returning a response with status 2xx. Try checking the request and response on the server side. Investigate their properties like req.query and the foundInk object you are returning. Also, try making the request to your server via another client, such as cURL or postman, to see what exactly is happening.

            – Renato Vassão
            Mar 9 at 15:09











          • Ok, in postman I can do a GET to (localhost:3000/search?search=133) (or whatever number at the end) and get an object of the query returned, so there must be something wrong with my AJAX? If I do url: '/search' + '?search=' + query, type: 'GET', dataType: "json", success: function(data) $('.result').html(data.ink.ink); alert(data); , 133 will briefly show in my EJS and disappear, but the alert is "[object Object]"

            – nchiles
            Mar 9 at 15:36

















          • I made an AJAX request in my main.js by clicking search button in EJS file: $(".search-button").click(function() $.ajax( url: '/search', type: 'GET', dataType: "json", success: function(data) console.log(data); ); ); but it's logging null. If I do a get request straight from the form to /search, it returns my search result object. I know you said parse the json, but I'm not sure where to do that, or if that's why it's returning null from the AJAX. Thanks again for your help.

            – nchiles
            Mar 9 at 4:40











          • Try adding an error function to your AJAX call to print the error and send the result here. Also, in your main.js, check how are the parameters coming from the AJAX request and the browser request, to see if we are missing something.

            – Renato Vassão
            Mar 9 at 13:20











          • I added the error function, but I'm not getting an error back. To check how the parameters are coming from AJAX and browser I tried this, to make sure the query was being sent to the AJAX and am getting an alert now with the query: $(".search-button").click(function() *query = $('#autocompleteInks').val()* $.ajax( url: '/search' + '?search=' + query, type: 'GET', dataType: "json", success: function(data) alert(data.ink.ink); , error: function(err) console.log(err); ); ); but I can't use <%=ink.ink %> or in EJS

            – nchiles
            Mar 9 at 14:55











          • Ok, so if the success function is executing it means that your server is returning a response with status 2xx. Try checking the request and response on the server side. Investigate their properties like req.query and the foundInk object you are returning. Also, try making the request to your server via another client, such as cURL or postman, to see what exactly is happening.

            – Renato Vassão
            Mar 9 at 15:09











          • Ok, in postman I can do a GET to (localhost:3000/search?search=133) (or whatever number at the end) and get an object of the query returned, so there must be something wrong with my AJAX? If I do url: '/search' + '?search=' + query, type: 'GET', dataType: "json", success: function(data) $('.result').html(data.ink.ink); alert(data); , 133 will briefly show in my EJS and disappear, but the alert is "[object Object]"

            – nchiles
            Mar 9 at 15:36
















          I made an AJAX request in my main.js by clicking search button in EJS file: $(".search-button").click(function() $.ajax( url: '/search', type: 'GET', dataType: "json", success: function(data) console.log(data); ); ); but it's logging null. If I do a get request straight from the form to /search, it returns my search result object. I know you said parse the json, but I'm not sure where to do that, or if that's why it's returning null from the AJAX. Thanks again for your help.

          – nchiles
          Mar 9 at 4:40





          I made an AJAX request in my main.js by clicking search button in EJS file: $(".search-button").click(function() $.ajax( url: '/search', type: 'GET', dataType: "json", success: function(data) console.log(data); ); ); but it's logging null. If I do a get request straight from the form to /search, it returns my search result object. I know you said parse the json, but I'm not sure where to do that, or if that's why it's returning null from the AJAX. Thanks again for your help.

          – nchiles
          Mar 9 at 4:40













          Try adding an error function to your AJAX call to print the error and send the result here. Also, in your main.js, check how are the parameters coming from the AJAX request and the browser request, to see if we are missing something.

          – Renato Vassão
          Mar 9 at 13:20





          Try adding an error function to your AJAX call to print the error and send the result here. Also, in your main.js, check how are the parameters coming from the AJAX request and the browser request, to see if we are missing something.

          – Renato Vassão
          Mar 9 at 13:20













          I added the error function, but I'm not getting an error back. To check how the parameters are coming from AJAX and browser I tried this, to make sure the query was being sent to the AJAX and am getting an alert now with the query: $(".search-button").click(function() *query = $('#autocompleteInks').val()* $.ajax( url: '/search' + '?search=' + query, type: 'GET', dataType: "json", success: function(data) alert(data.ink.ink); , error: function(err) console.log(err); ); ); but I can't use <%=ink.ink %> or in EJS

          – nchiles
          Mar 9 at 14:55





          I added the error function, but I'm not getting an error back. To check how the parameters are coming from AJAX and browser I tried this, to make sure the query was being sent to the AJAX and am getting an alert now with the query: $(".search-button").click(function() *query = $('#autocompleteInks').val()* $.ajax( url: '/search' + '?search=' + query, type: 'GET', dataType: "json", success: function(data) alert(data.ink.ink); , error: function(err) console.log(err); ); ); but I can't use <%=ink.ink %> or in EJS

          – nchiles
          Mar 9 at 14:55













          Ok, so if the success function is executing it means that your server is returning a response with status 2xx. Try checking the request and response on the server side. Investigate their properties like req.query and the foundInk object you are returning. Also, try making the request to your server via another client, such as cURL or postman, to see what exactly is happening.

          – Renato Vassão
          Mar 9 at 15:09





          Ok, so if the success function is executing it means that your server is returning a response with status 2xx. Try checking the request and response on the server side. Investigate their properties like req.query and the foundInk object you are returning. Also, try making the request to your server via another client, such as cURL or postman, to see what exactly is happening.

          – Renato Vassão
          Mar 9 at 15:09













          Ok, in postman I can do a GET to (localhost:3000/search?search=133) (or whatever number at the end) and get an object of the query returned, so there must be something wrong with my AJAX? If I do url: '/search' + '?search=' + query, type: 'GET', dataType: "json", success: function(data) $('.result').html(data.ink.ink); alert(data); , 133 will briefly show in my EJS and disappear, but the alert is "[object Object]"

          – nchiles
          Mar 9 at 15:36





          Ok, in postman I can do a GET to (localhost:3000/search?search=133) (or whatever number at the end) and get an object of the query returned, so there must be something wrong with my AJAX? If I do url: '/search' + '?search=' + query, type: 'GET', dataType: "json", success: function(data) $('.result').html(data.ink.ink); alert(data); , 133 will briefly show in my EJS and disappear, but the alert is "[object Object]"

          – nchiles
          Mar 9 at 15:36













          0














          Ideally you make an endpoint like this.



          ( id parameter is optional here...thats why the '?' ) 
          www.example.com/api/inks/:id?


          // return all the inks
          www.example.com/api/inks

          // return a specific ink with id=2
          www.example.com/api/inks/2


          So now you can render all the links via /inks and search a particular ink by using the endpoint /ink/:id?



          Hope this helps !






          share|improve this answer























          • Thank you for your help. Right now my endpoint for all inks is www.example.com/ and when I search, the endpoint is www.example.com/?search=123. I'm confused on the part where I'm rendering the page and sending ink: foundInk or ink: allInks. Is there a way to use those as variable and send both with the endpoint www.example.com/2? Sorry if I'm not making sense.

            – nchiles
            Mar 8 at 18:31











          • Yeaah...I am not getting what you're trying to say. Use what as a variable and send what and why to the endpoint /2 ?

            – Hemant Parashar
            Mar 8 at 18:51











          • I was using /2 from your example as the endpoint. And I was wondering if there was a way to use the result from the search as a variable, to use on the client side. Basically, I don't understand how (at any endpoint), there can be results from Ink.find() and Ink.find(ink: req.query.search). Would it help to show my ejs file?

            – nchiles
            Mar 8 at 19:37















          0














          Ideally you make an endpoint like this.



          ( id parameter is optional here...thats why the '?' ) 
          www.example.com/api/inks/:id?


          // return all the inks
          www.example.com/api/inks

          // return a specific ink with id=2
          www.example.com/api/inks/2


          So now you can render all the links via /inks and search a particular ink by using the endpoint /ink/:id?



          Hope this helps !






          share|improve this answer























          • Thank you for your help. Right now my endpoint for all inks is www.example.com/ and when I search, the endpoint is www.example.com/?search=123. I'm confused on the part where I'm rendering the page and sending ink: foundInk or ink: allInks. Is there a way to use those as variable and send both with the endpoint www.example.com/2? Sorry if I'm not making sense.

            – nchiles
            Mar 8 at 18:31











          • Yeaah...I am not getting what you're trying to say. Use what as a variable and send what and why to the endpoint /2 ?

            – Hemant Parashar
            Mar 8 at 18:51











          • I was using /2 from your example as the endpoint. And I was wondering if there was a way to use the result from the search as a variable, to use on the client side. Basically, I don't understand how (at any endpoint), there can be results from Ink.find() and Ink.find(ink: req.query.search). Would it help to show my ejs file?

            – nchiles
            Mar 8 at 19:37













          0












          0








          0







          Ideally you make an endpoint like this.



          ( id parameter is optional here...thats why the '?' ) 
          www.example.com/api/inks/:id?


          // return all the inks
          www.example.com/api/inks

          // return a specific ink with id=2
          www.example.com/api/inks/2


          So now you can render all the links via /inks and search a particular ink by using the endpoint /ink/:id?



          Hope this helps !






          share|improve this answer













          Ideally you make an endpoint like this.



          ( id parameter is optional here...thats why the '?' ) 
          www.example.com/api/inks/:id?


          // return all the inks
          www.example.com/api/inks

          // return a specific ink with id=2
          www.example.com/api/inks/2


          So now you can render all the links via /inks and search a particular ink by using the endpoint /ink/:id?



          Hope this helps !







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 8 at 17:45









          Hemant ParasharHemant Parashar

          883414




          883414












          • Thank you for your help. Right now my endpoint for all inks is www.example.com/ and when I search, the endpoint is www.example.com/?search=123. I'm confused on the part where I'm rendering the page and sending ink: foundInk or ink: allInks. Is there a way to use those as variable and send both with the endpoint www.example.com/2? Sorry if I'm not making sense.

            – nchiles
            Mar 8 at 18:31











          • Yeaah...I am not getting what you're trying to say. Use what as a variable and send what and why to the endpoint /2 ?

            – Hemant Parashar
            Mar 8 at 18:51











          • I was using /2 from your example as the endpoint. And I was wondering if there was a way to use the result from the search as a variable, to use on the client side. Basically, I don't understand how (at any endpoint), there can be results from Ink.find() and Ink.find(ink: req.query.search). Would it help to show my ejs file?

            – nchiles
            Mar 8 at 19:37

















          • Thank you for your help. Right now my endpoint for all inks is www.example.com/ and when I search, the endpoint is www.example.com/?search=123. I'm confused on the part where I'm rendering the page and sending ink: foundInk or ink: allInks. Is there a way to use those as variable and send both with the endpoint www.example.com/2? Sorry if I'm not making sense.

            – nchiles
            Mar 8 at 18:31











          • Yeaah...I am not getting what you're trying to say. Use what as a variable and send what and why to the endpoint /2 ?

            – Hemant Parashar
            Mar 8 at 18:51











          • I was using /2 from your example as the endpoint. And I was wondering if there was a way to use the result from the search as a variable, to use on the client side. Basically, I don't understand how (at any endpoint), there can be results from Ink.find() and Ink.find(ink: req.query.search). Would it help to show my ejs file?

            – nchiles
            Mar 8 at 19:37
















          Thank you for your help. Right now my endpoint for all inks is www.example.com/ and when I search, the endpoint is www.example.com/?search=123. I'm confused on the part where I'm rendering the page and sending ink: foundInk or ink: allInks. Is there a way to use those as variable and send both with the endpoint www.example.com/2? Sorry if I'm not making sense.

          – nchiles
          Mar 8 at 18:31





          Thank you for your help. Right now my endpoint for all inks is www.example.com/ and when I search, the endpoint is www.example.com/?search=123. I'm confused on the part where I'm rendering the page and sending ink: foundInk or ink: allInks. Is there a way to use those as variable and send both with the endpoint www.example.com/2? Sorry if I'm not making sense.

          – nchiles
          Mar 8 at 18:31













          Yeaah...I am not getting what you're trying to say. Use what as a variable and send what and why to the endpoint /2 ?

          – Hemant Parashar
          Mar 8 at 18:51





          Yeaah...I am not getting what you're trying to say. Use what as a variable and send what and why to the endpoint /2 ?

          – Hemant Parashar
          Mar 8 at 18:51













          I was using /2 from your example as the endpoint. And I was wondering if there was a way to use the result from the search as a variable, to use on the client side. Basically, I don't understand how (at any endpoint), there can be results from Ink.find() and Ink.find(ink: req.query.search). Would it help to show my ejs file?

          – nchiles
          Mar 8 at 19:37





          I was using /2 from your example as the endpoint. And I was wondering if there was a way to use the result from the search as a variable, to use on the client side. Basically, I don't understand how (at any endpoint), there can be results from Ink.find() and Ink.find(ink: req.query.search). Would it help to show my ejs file?

          – nchiles
          Mar 8 at 19:37

















          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.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55068182%2fget-request-with-different-mongo-find-results-on-same-page%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

          Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

          Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

          2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived