pretty url search string instead of question marks - node.js

I have been looking at other websites and see when you search, primary something like a search term, location and category you will see a pretty url like:
example.com/black-boots/new-york/shoes
instead of what I have now which is something like:
example.com/search-results/search?=black+boots&city=new+york&category=shoes
In my route I could start with something like:
router.get('/search-results/:search/:city/:category', shopController.getSearchResults);
And in the controller I could use req.params.city and so on to get the values from the url but the part that I can't figure out is a good way to get the text input values into the url using a get request.
Using GET by default gives me the 'ugly' looking url.
Basically the part that needs to go into the form
<form method="GET" action="/search-results/search/city/category">

Comments, plus this code sample for a GET request:
const form = document.getElementById('searchform');
form.addEventListener('submit', evt => {
const who = encodeURIComponent(document.getElementById('who').value);
const where = encodeURIComponent(document.getElementById('where').value);
const what = encodeURIComponent(document.getElementById('what').value);
window.location.href = `/${who}/${where}/${what}`;
}

Related

Mongoose model deleteOne() only works with hard coded string

I have a strange error with mongoose deleteOne() function. Today I wanted to work on my project and got an error while deleting an item from a collection. It simply doesn't delete the document until I use a hardcoded parameter for the options object like this:
const { deletedCount } = await Model.deleteOne({symbol: 'hardcoded'})
// results in deletedCount = 1
But if I try to use a dynamic string like:
const test = 'dynamic'
const { deletedCount } = await Model.deleteOne({symbol: test})
// results in deletedCount = 0
It does no longer delete the document from my collection. The strange thing is yesterday it worked fine and deleted the item.
I tried one other thing I read regarding errors with deleteOne():
const { deletedCount } = await Model.deleteOne({symbol: JSON.stringifiy(symbol)})
But this doesn't work, too.
Does anyone have an idea what's going wrong?
I always default to using ids whenever possible to make sure there's no mistake in the data I am targeting with a given operation.
So in this case that would mean using findByIdAndDelete() instead.
If I don't know the id of the document I'm trying to delete, then only I'd use findOneAndDelete() or deleteOne(), as you have, with something other than an id to identify the document I'm looking for.
Are you certain that the key-value pair you're passing to the function exists in your database?
Problem solved. I accidentally added an additional space character at the end of the string. This is very strange because the error was there since the beginning of my project and yesterday it worked.
So for everyone who might have a similar problem:
I have a ejs template file where I render a html element like this:
<div id="<%= symbol %> ">
Then in my event handler for requesting the server to deleting one item from my list I use the id attribute as a request parameter in the body. In the route handler this parameter is passed to mongoose:
const { symbol } = req.body
const { deletedCount } = await Model.deleteOne({ symbol })
As I mentioned. In the template file after the last ejs seperator there is an addional space character that caused the error. I spotted this issue by making a copy of the monoogse query and than logged it to the console. There I could see the wrong condition parameter.

append a value to existing url in reactjs

I have the following url and a parameter :
number:200
"http://localhost:8000/textpath"
I want the path to be like this:
"http://localhost:8000/textpath/200"
How do I do that using Reactjs?I want to use the appended url in fetch method as follows:
fetch("http://localhost:8000/textpath/200")
A simple append did the work.Doesn't have to complicate
fetch("http://localhost:8000/textpath/"+number)
Try using Template literals.
const url = `http://localhost:8000/textpath/${number}`
fetch(url);
where number = 200
Refer here for more information

Passing JavaScript variable into snippet

I'm working on a search form for my ModX application that is consisted of a chunk and a snippet. What I'm trying to achieve is to pass what was entered into the search box into a javascript variable and then pass it to my snippet, however, the snippet receives the literal text, and not the value that I enter into the parameter when I call it.
I don't know if what I'm attempting is possible in ModX or if I need to take a different approach, but I would be hugely thankful for anyone who can provide any insight.
Chunk:
<script>
$('.search-btn').click(function() {
var search = $('.search-entry').val();
[[showSearchResults? &q=`search`]]
});
</script>
Snippet:
<?php
$search = $modx->getOption('q', $scriptProperties);
echo $search; // this always prints "search"
?>
I doubt that this code makes sense:
<script>
$('.search-btn').click(function() {
var search = $('.search-entry').val();
[[showSearchResults? &q=`search`]]
});
</script>
The snippet call returns the result of snippet's execution with param q always equal to the string 'search' in your case and finally on your page you will have something like this:
<script>
$('.search-btn').click(function() {
var search = $('.search-entry').val();
'search' // assuming your snippet just returns what has been passed to it.
});
</script>
In order to accomplish your task you can use a simple trick. Call your snippet like this:
[[!yourSnippet? &yourVar=`[[!#POST.yourVar]]` ]] // or GET
Lets say this snippet call is located on a page accessible via url /test/ on your server. So, now you just have to send the parameters you collected from your search form using AJAX to the /test/ page where your snippet is:
var yourVar = $('.search-entry').val();
$.ajax({
type: "POST",
url: "/test/",
data: {yourVar: yourVar},
success: success,
dataType: "html"
});
Hope it helps :)
PS If you want to search Resource content and TV content, I can highly recommend an extra called SimpleSearch.

URL rewriting using NodeJS

I want to customize my URL rewriting but it seems not working as I'd like it to.
My old code :
var rewrite = require('express-urlrewrite');
exports.rewrite = function(app){
app.use(rewrite('/p/:id/:seoUrl', '/page/show/$1/$2'));
}
In my browser : http://mysite/p/1/seo-title (this URL works)
My new code :
var rewrite = require('express-urlrewrite');
exports.rewrite = function(app){
app.use(rewrite('/:seoUrl', '/page/show/$1/$2'));
}
In my browser : http://mysite/seo-title (ID not found)
Using NodeJS, is there a way to exclude the ID in the URL?
well. if you take a close look at this: '/page/show/$1/$2' you might notice that it accepts two arguments.
since you removed :id you'd have to replace $1 with a static id or remove it completely aswell as you have to change $2 to $1
so in the end your code might look like this: app.use(rewrite('/:seoUrl', '/page/show/1/$1'));
or like this: app.use(rewrite('/:seoUrl', '/page/show/$1'));

Codeigniter url rewrite issue when using pagination

I am learning codeigniter these days. I have done a website for my own use a couple of days ago where i have just applied pagination today but it seems there is problem with my url rewriting.
Here is my action inside my controller which grabs list of authors in a category
`public function index() {
$data['main_content'] = "templates/public/audio/author_view";
$data['authors'] = $this->author_model->get_authors();
$this->load->view("templates/public/template",$data);
}`
Here is my function that grabs value from db located inside my author model
`public function get_authors($type = 0) {
$this->db->select("author.name,author.image,author.slug");
$this->db->from("author");
$this->db->join("album","author.id = album.author_id");
$this->db->where("album.mime_type",$type);
$this->db->group_by("author.name");
$query = $this->db->get();
return $query->result();
}`
When i clicked on one of author grabbed it open all albums of selected author. then url looks link www.xyz.com/audio/album-nameHere is my code for this route.
$route['audio/(:any)'] = "audio/view_author_album";
At this stage it works fine. But now today when i applied pagination i found that this route will not do more work for me. I have added pagination in my index action Below you can see my code
public function index() {
$config['base_url'] = "http://localhost/mywebsite/audio/index/";
$config['total_rows'] = $this->author_model->get_total_rows();
$config['per_page'] = 1;
$this->pagination->initialize($config);
$data['main_content'] = "templates/public/audio/author_view";
$data['authors'] = $this->author_model->get_authors($config['per_page'], $this->uri->segment(3));
$this->load->view("templates/public/template",$data);
}
This open the details http://localhost/mysite/audio/index/2
against this url my route rule $route['audio/(:any)/(:any)'] = "audio/view_album_details"; works. It should grab the next page instead of my detail view. And url should be something like http://localhost/mysite/audio/2
I also tried $route['audio/(:num)'] = "audio/;
I will highly appropriate if anyone can help me in solving this problem.
i encounter the same issue and i solve it by using two same router
$route['a-b-c/([a-z0-9-]+)/([0-9]*)'] = "product/category/$1";
$route['a-b-c/([a-z0-9-]+)'] = "product/category/";

Resources