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?
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
add a comment |
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
add a comment |
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
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
node.js mongodb express mongoose
asked Mar 8 at 17:29
nchilesnchiles
34
34
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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
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 dourl: '/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
|
show 6 more comments
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 !
Thank you for your help. Right now my endpoint for all inks iswww.example.com/
and when I search, the endpoint iswww.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 endpointwww.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 fromInk.find()
andInk.find(ink: req.query.search)
. Would it help to show my ejs file?
– nchiles
Mar 8 at 19:37
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
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 dourl: '/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
|
show 6 more comments
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
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 dourl: '/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
|
show 6 more comments
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
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
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 dourl: '/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
|
show 6 more comments
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 dourl: '/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
|
show 6 more comments
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 !
Thank you for your help. Right now my endpoint for all inks iswww.example.com/
and when I search, the endpoint iswww.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 endpointwww.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 fromInk.find()
andInk.find(ink: req.query.search)
. Would it help to show my ejs file?
– nchiles
Mar 8 at 19:37
add a comment |
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 !
Thank you for your help. Right now my endpoint for all inks iswww.example.com/
and when I search, the endpoint iswww.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 endpointwww.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 fromInk.find()
andInk.find(ink: req.query.search)
. Would it help to show my ejs file?
– nchiles
Mar 8 at 19:37
add a comment |
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 !
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 !
answered Mar 8 at 17:45
Hemant ParasharHemant Parashar
883414
883414
Thank you for your help. Right now my endpoint for all inks iswww.example.com/
and when I search, the endpoint iswww.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 endpointwww.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 fromInk.find()
andInk.find(ink: req.query.search)
. Would it help to show my ejs file?
– nchiles
Mar 8 at 19:37
add a comment |
Thank you for your help. Right now my endpoint for all inks iswww.example.com/
and when I search, the endpoint iswww.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 endpointwww.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 fromInk.find()
andInk.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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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