"req.user" Appears to be unresolved but isn't - javascript

I'm currently working on an Express.js application and I'm using Passport.js (and specifically passport-local) for authentication into what's going to be an administration panel.
As of right now, the program does indeed work. I have my app.js that initializes passport and sets the proper sessions etc. I also have my routes going to a separate file in my routes folder from the app.js file.
I'm not sure if this is a bug with Jetbrain's IntelliJ Idea/WebStorm or if I am following bad code practices, but I am getting a "Unresolved variable user" error in my IDE when I call "req.user" in my index.js file.
https://i.imgur.com/zgw3xUj.png (Can't post images due to reputation)
In my index.ejs file, I am also getting a similar error in my IDE.
https://i.imgur.com/5bt7xf9.png
Even though these 2 issues are showing, the "req.user" variable still consists of data and I am able to successfully check if "user" exists in my ejs file and output the adequate information (when logged in.)
In my app.js file, I am able to call the "req.user" variable without the IDE showing any unknown variable error (https://i.imgur.com/80rzZmh.png, line 48), so I am under the belief that I probably did not setup my routes properly.
Any help is appreciated.
Thanks.

An IDE like webstorm/intelliJ/textmate/sublime tries to help you with the information you provided it. Since you are using passport, req.user might be populated, but intelliJ doesn't know that it is. You can help your IDE by providing additional typings like #typings/passport.

Related

strange issue of undefined in url in UAT env. in local it is working:

can anybody please help on this.
I have strange issue in API. My backend is created in php and UI in react.
in my env file below path are present.
API_URL=http://localhost:8080
FRONT_URL=http://localhost:80
so API_URL is for backend and FRONT_URL is for UI. I have called my API like below in react.
axios
.get(process.env.API_URL+ '/code';)
.then()
.error
my code is working properly. same API I can access from reactas well as postman. there is no issue.
But when we deployed our application in UAT URL is appending undefined between API name and API URL.
Below are API value in UAT env file.
FRONT_URL=https://uat-solve.dell.com
API_URL=https://api.uat-solve.dell.com
Now my application throwing error. it is not running. when I checked what URL it is trying to reach out then it came below.
https://api.uat-solve.dell.com/undefined/code
It is appending undefined in between. when I tried to access same url from postman after removing undefined output is coming.
What could be the possible reason of it? this is the new component in react. existing application working properly
Edit 1:-
I changed env variable to REACT_APP* but still it did not work.
The Create React App documentation states that you must prefix all environment variables within your .env files with REACT_APP_ for them to be available from within your code process.env.REACT_APP_<variable name>
If you don't want to use REACT_APP_ prefix, you can use env-create-react-app
Try using backticks(``)
For Example : `${process.env.API_URL}/code`
Make sure you have the .env file in your root folder(same place where you have your package.json) and NOT in your src folder.
You mention you changed your environment variables to start with REACT_APP - do you have a trailing underscore? They should start with REACT_APP_.

Strapi custom auth controller

I'm using Strapi for my API and Back office. Everything works fine except one thing: I can't figure out how to override the controller that is used for the forgot password feature. I've tried to follow the documentation, especially this page : https://strapi.io/documentation/3.0.0-beta.x/admin-panel/customization.html#development-mode but no chance.
Here what I've tried :
Create a folder admin at the root of the project, inside which I created controller/Auth.js. In this file I created my custom forgotPassword function but it's not called.
Add the file admin/config/routes.json, my controller got the same name but I thought that maybe I need to repeat the route here to override, still not successful.
I saw on some page that to get what I was searching I needed to put those files (config/routes.json and controller/Auth.js) inside /extensions/user-permissions/admin, but it's still not working.
Whatever I try, it's always the default forgot password controller that is called, from the strapi-admin node modules.
Any help would be greatly appreciated, I don't see what I'm missing here.
It's normal, because your are not writing the file in the right place.
So I will help you with that.
First here is the documentation for the customization - https://strapi.io/documentation/3.0.0-beta.x/concepts/customization.html#plugin-extensions
Then we have to find the file in the code source we want to update.
Here is the function - https://github.com/strapi/strapi/blob/master/packages/strapi-plugin-users-permissions/controllers/Auth.js#L266
Based on the file path and the way to customize in strapi.
You will have to create a file to this path extensions/users-permissions/controllers/Auth.js
Then create a module.exports with source code function and the update it.
this should work

Getting 404 for local files in Node.js

I recently posted Background image not showing with CSS and thought it has to be something with the url I included even though I was sure it was correct (they were in the same directory so it was straightforward). I wasn't able to resolve that issue and now, I face the same issue but for a .js file.
Picture of work space:
Picture of the Run prompt:
.
In app.js I do have require('./like_button');. I am new to using these technologies, I must be missing something general...
You have to serve the static file from a specific folder. In your case views.
Add this line where you've declared your app.
app.use(express.static('views'))
For more reference check out
http://expressjs.com/en/starter/static-files.html

External javascript in html is sent incorrectly to the server

I'm running a Node.js server along with an Angular frontend. One of the Angular dependencies I'm using requires me to import a javascript file into my html page, by the name of swing.js. However, when I try to do this, it sends the required file as an http request to the server, resulting in requests that look like the following:
http://localhost:3000/home/me/app/node_modules/angular-swing/dist/swing.js
Obviously, this comes up as a 404. As an alternative, I've tried changing
<script src="/home/me/app/node_modules/angular-swing/dist/swing.js"></script>
into
<script src="swing.js"></script>
and then on the server-side, doing:
app.get('swing.js', function(req, res){
res.sendFile('home/me/app/node_modules/angular-swing/dist/swing.js');
});
This works a little more, but then the file doesn't run properly, as I'm assuming it's no longer in the npm environment it needs to be in. I've tried multiple iterations of changing
<script src="/home/me/app/node_modules/angular-swing/dist/swing.js"></script>
into something that uses periods (.) to represent more relative paths, but that doesn't appear to work either. Overall, I'm very stuck, and would appreciate any insight. If it's of any use, I'm also using:
app.use(express.static(__dirname+'/public'));
Making my comments into an answer...
node.js does not serve any files by default so any script files that you need sent from your server to the client upon request need an explicit route to handle them or they need some generic route that knows how to handle all the requested script files.
swing.js in the client is not part of any "NPM environment". It's running the browser at that point, not in any NPM enviornment. It's possible that swing.js itself needs some other scripts or resources that also need routes and that's why it doesn't work after you make an explicit route for it. You can probably examine the specific errors in the console to give you a clue why it isn't working.
You may also want to read this: How to include scripts located inside the node_modules folder?

Can I put css and html markup inside a js flashify message

I am working on a project built with sails.js. I have taken over this project and I'm not very familiar with sails.js yet so I'm still getting my bearings.
From what I can see, the sails package includes flashify and the previous developer is using it in several places to display notification. The client wants a few of those notifications to be a little fancier than just the default popup and I'm wondering if it's possible to somehow restyle the flashify popup and add html markup to it.
Here's one instance where a message is initiated:
if(!req.isAuthenticated()){
req.flash("error", "Please log in");
return res.redirect('back');
}
I have tried adding html tags to the message string, escaping them and taking them out of the string quotes but none of that worked. Is it possible at all?
A couple of things:
Sails uses connect-flash by default. Your project might actually be using Flashify, but if you don't actually see require('flashify') in the code, it probably isn't.
More importantly, both connect-flash and flashify are back-end packages that just save messages into the session so that they survive between requests. They have nothing to do with the actual display of the message, which is handled in your template (most likely an .ejs file in the views folder). If there's a popup, then that's being created and launched in that template, so that's where you'd want to style it. Take a look at the Sails views documentation for more about view rendering, and also this question about rendering flash messages in a view.

Categories

Resources