Missing hash prefix error in MEAN stack - node.js

I am getting
Error: [$location:ihshprfx] Invalid url "http://localhost:3000/#", missing hash prefix "#!". http://errors.angularjs.org/1.2.16/$location/ihshprfx?p0=http%3A%2F%2Flocalhost%3A3000%2F%23&p1=%23
after delete function:
$scope.delete = function(venue) {
venue.$remove();
};
but venue is successfully removed. What could be wrong?
UPDATE: I saw that in a dropdown I wrote href = '#' . But when I delete it, nothing happens. How I can call directive from a tag?
Delete

Your ng-click calls a function called remove while your scope method is titled delete. Because it can't find the function you've set, I believe Angular doesn't to prevent the default event, which sends the user to '#', which is not valid based on your hash prefix settings.
If you match the function call to the method name, I think you should be fine.

Related

router.get(), use only part of url?

Currently I have this code:
router.get('/admins', function(res,req) {
However, I want it to be when someone goes to say, 'localhost:5000/admins/54323', I want the node-js app to notice 'hey, they're requesting for an admins list! lets find it'. However, with the router.get() function it only works if it is exactly that, is there a way to have it so if only the start is /admins then it sets a variable for the final part?
Through more research, I found an answer. I just put /:id after the /admins bit,
you can access the rest of the string in req.params.id where .id is the same as the string written after the : sign

expressjs pattern to match the rest of the path

I'm trying to create an endpoint that contains an actual path that I extract and use as a parameter. For instance, in the following path:
/myapi/function/this/is/the/path
I want to match "/myapi/function/" to my function, and pass the parameter "this/is/the/path" as the parameter to that function.
If I try this it obviously doesn't work because it only matches the first element of the path:
app.get("/myapi/function/:mypath")
If I try this it works, but it doesn't show up in req.params, I instead have to parse req.path which is messy because the logic has to know about the whole path, not just the parameter:
app.get("/myapi/function/*")
In addition, the use of wildcard routing seems to be discouraged as bad practice. I'm not sure I understand what alternative the linked article is trying to suggest, and I'm not using the query as part of a database call nor am I uploading any information.
What's the proper way to do this?
You can use wildcard
app.get("/myapi/function/*")
And then get your path
req.params[0]
// Example
//
// For the route "/myapi/function/this/is/my/path"
// You will get output "this/is/my/path"

Why does return RedirectToAction("/Index") work and RedirectToAction("Index)" not work

For a long time, I've had an action that returned
return RedirectToAction("Index","Vendor");
and it worked as expected. At the completion of this function, my Index function was called.
However, lately it stopped working. That same line of code now directs the browser to the url localhost:67676/Vendor/, my Index() action is never called and the page displays:
HTTP Error 403.14 - Forbidden
The Web server is configured to not list the contents of this directory.
However, if i add a forward slash to the method parameters like
return RedirectToAction("/Index","Vendor");
Everything works as expected and the Index function is called.
Any idea why I need to use "/Index" now but "Index" worked before
The error means that you have a folder in your app named Vendor and the url is trying to navigate to that folder rather than your VendorController. To solve the problem, rename the folder so that it does not match a controller name.
To understand what is happening behind the scenes,
return RedirectToAction("Index","Vendor"); internally looks at your route definitions for a match, and it matches your default route which has defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }. Because you pass "Vendor" as the controller, which does not match default, the first segment of the url becomes Vendor. And because your pass "Index" as the action, it matches the route, so no additional segment is added (its not required). The final url becomes localhost:67676/Vendor which matches your folder (hence the error).
When you used return RedirectToAction("/Index","Vendor");, your passing "/Index" which does not match the default action ("/Index" != "Index") so it generates a 2nd segment in the url which now becomes localhost:67676/Vendor/Index which will hit the controller because you don't (and could not) have a folder named "Vendor/Index"
It's a little late, but I'm posting it under this thread because I ran into the same problem. If there is a folder with the same name as Controller in your project file, you are encountering this problem.
Example;
VendorController -
Vendor (Folder Name)
I encountered this problem because of the wrong path I wrote when I wanted to create a new folder to save an image file.
Be careful when creating "Server.MapPath".

get method with colon and question mark in server path

Follow up question: What is the code "res.json(false);" doing? Doesn't that print out false on the page instead of showing the data I want?
I'm looking at the following sample code. I understand that .get( is the method and /:characters? is the server path. In this search, what is the point of the colon and the question mark in the path? Shouldn't the question mark come before characters because it is a query?
app.get('/:characters?', function (req, res) {
var chosen = req.params.characters;
if (chosen) {
console.log(chosen);
for (var i = 0; i < characters.length; i++) {
if (chosen === characters[i].routeName) {
res.json(characters[i]);
return;
}
}
res.json(false);
} else {
res.json(characters);
}
});
In this case the question mark signifies an optional parameter "characters". This allows for an endpoint that MAY have a value or not. They are then testing against that parameter to see if it was included. If so they will iterate through the "characters" object and return any matching entry to the endpoint the user specified.
Simplest answer:
:XXX means that its a URL parameter. (i.e. req.params.XXX will pick up what the XXX is)
? means that the parameter is optional. (i.e. the client-side user doesn't need to include this parameter in the url).
So:
/:characters? would allow for: both / AND /yoda to hit this route.
:characters isn't actually part of the query string. It will be part of the url.
The url will be something similar to the following (assuming you are running this server locally and on port 8080):
http://localhost:8080/abcdefg
And in that case, req.params.characters will be 'abcdefg'
Putting an explicit question mark in the route definition is a mistake, in my opinion. I'm not entirely sure what purpose that question mark would serve.
For the follow up question, what it appears to be doing is looking for a match in the characters variable (which I assume is defined externally) by characters[i].routeName, and returning the found value. If no value is found, that's when it sends back false (or tries to - to be honest, I'm not sure what express will do if you try using res.json(false), since I'm not sure false is valid JSON).
This is a normal case that a endpoint or a resource can contain a variable that means
lets take facebook for example so if you see the timeline of MarkZukerberg then the endpoint or url is
https://www.facebook.com/zuck
if you see your own profile then that zuck will be replaced by your name. so the name parameter here is a variable.
When ever a variable is a part of the url then we precede that with a colon sign in express syntax
and if we want to send some other values as the query parameter then we use ? mark to tell the server that the following string will be a query parameter
?name=value&age=12&gender=male
And
res.json(false)
will just return false as the response nothing else

Mixing Static Strings with Views Arguments

Can anyone tell me if it is possible to mix views arguments with static strings?
For example in the path section of a view feed display I need:
/mypath/%.xml
with the ".xml" part being the static string.
Thanks in advance.
I figured it out finally.
Under validation, choose PHP code. Then I entered:
// strip ".xml" from incoming
$new_arg = preg_replace('/\.xml$/', '', $argument ); argument
$handler->argument = $new_arg;
return TRUE; //must return something
That works. Now Drupal sends "foo" to the SQL query, even if the incoming argument via the url is "foo.xml"
I just tested this, and you can't do the exact path you posted above. Views appears to only recognize '%' as an argument placeholder if it sits between slashes, or by itself at the end. So, what will work is something like this:
/mypath/%/rss.xml
or
/mypath/static/%
In path, anyway, you should set path/%
But you can check argument %.xml in validating code:
In views argument additing/editing window:
Validator options - Validator - PHP Code:
Enter PHP code that returns TRUE or FALSE. No return is the same as FALSE, so be SURE to return something if you do not want to declare the argument invalid. Do not use . The argument to validate will be "$argument" and the view will be "$view". You may change the argument by setting "$handler->argument".
Use strpos to check if there xml string.
Also you can modify argument as it wrote in comment: $handler->argument

Resources