Make redirection in express post, by react - node.js

I am using react and its router to make the redirect of my application. When I use a get instruction in my server express it works fine. But when I make a post instruction, it doesn't redirect. What I have to do?. Here is my code:
(I receive the response in the way that I expect, but after that, nothing happens)
lisaApp.post('/signup', (req, res) => {
const validationResult = validateSignupForm(req.body);
if (!validationResult.success) {
return res.status(400).json({
success: false,
message: validationResult.message,
errors: validationResult.errors
});
}
var newClient = req.body
newClient.clientemailname = newClient.userclient
newClient.client_id = uuid.v4()
delete newClient.confirmPassword
ClientApi.saveClient(newClient, function (err, usr) {
if (err) return res.status(500).send(err.message)
console.log('ready to redirect') //Here, the response has a status "0k"
res.redirect('/')
});
});

You can write your api like this:
lisaApp.post('/signup', (req, res) => {
const validationResult = validateSignupForm(req.body);
if (!validationResult.success) {
return res.status(400).json({
success: false,
message: validationResult.message,
errors: validationResult.errors
});
}
var newClient = req.body
newClient.clientemailname = newClient.userclient
newClient.client_id = uuid.v4()
delete newClient.confirmPassword
ClientApi.saveClient(newClient, function (err, usr) {
if (err) return res.status(500).send(err.message)
console.log('ready to redirect') //Here, the response has a status "0k"
return res.status(200).json({
success:true,
redirectUrl: '/'
})
});
});
And in your React file you can use this code in your POST handler to redirect using react-router.
this.history.pushState(null, res.redirectUrl);

Related

req.params returns undefiened

I'm trying to get back the ID from the params but it keeps sending back undefiened, what would be the problem here and how can i solve it ?
this is the route:
app.delete(`${api_version}/delete-branch/:id`, verifyToken, branches.deleteBranch)
this is the controller:
exports.deleteBranch = (req, result) => {
const {branch_id} = req.params
console.log(branch_id) // => returns undefined
if(branch_id === undefined) {
result.status(404).send({
message: 'This branch does not exist',
statusCode: 404
})
} else {
// console.log(req.params)
Branches.deleteBranch(branch_id, (err, data) => {
if (err) {
result.status(500).send({
message: err.message
})
} else {
result.status(200).send({
message: 'Branch deleted successfully',
statusCode: 200,
data
})
}
})
}
}
You need to destruct req.params like this:
const {id} = req.params
instead of:
const {branch_id} = req.params
Or either defined the route as follow:
app.delete(`${api_version}/delete-branch/:branch_id`, verifyToken, branches.deleteBranch)
and then destruct const {branch_id} = req.params;

Cast to ObjectId failed for value ... at path "_id" for model but I am not doing any query

I have an express route that gets call with axios from the frontend. The thing is, not matter what I put into the route I always get the same error:
"Cast to ObjectId failed for value "getTodosMisProductos" at path "_id" for model "local""
I'm not doing any query to mongoose in that route but in any other route where I make a query everything works fine.
I've checked the middleware but there is not any query to mongoose
getTodosMisProductos
router.get("/getTodosMisProductos", auth, async (req, res) => {
/*
try {
const data = await Local.findOne({ user: req.user.id }).populate("products.producto");
console.log(data);
if (!data) {
return res
.status(404)
.json({ errors: [{ msg: "No se encontro el local" }] });
}
return res.status(200).json(data.products);
} catch (error) {
console.log(req.user.id);
console.error("error en llamado");
return res.status(500).send("Server Error");
}
*/
console.log("algo");
return res.status(200).json({ msg: "success" });
});
the code commented is the code I need to use, I changed it for testing purposes but even with that simple new code I get the same error.
auth middleware
const jwt = require("jsonwebtoken");
const config = require("config");
module.exports = function (req, res, next) {
// Get token from header
const token = req.header("x-auth-token");
// Check if not token
if (!token) {
return res
.status(401)
.json({ msg: "No tienes autorización para hacer esto" });
}
// Verify token
try {
const decoded = jwt.verify(token, require("../config/keys").jwtSecret);
req.user = decoded.user;
next();
} catch (error) {
res.status(401).json({ msg: "El token es inválido" });
}
};
action from where the route gets called
export const getAllProductos = () => async (dispatch) => {
try {
console.log("Esto se llama");
const res = await axios.get("/api/local/getTodosMisProductos/");
dispatch({
type: SET_PRODUCTS,
payload: res.data,
});
} catch (err) {
const errors = err.response.data.errors;
if (errors) {
errors.forEach((error) => dispatch(setAlert(error.msg, "danger")));
}
}
};
The response status is always 500 (Internal Server Error)
EDIT
//#route GET api/local/:id
//#desc obtener local por id
//#access private
router.get("/:id", auth, async (req, res) => {
try {
const local = await Local.findById(req.params.id);
if (!local) {
return res
.status(404)
.json({ errors: [{ msg: "No se encontro el local" }] });
}
return res.status(200).json(local);
} catch (error) {
console.error(error.message);
res.status(500).send("Server Error");
}
});
You have another route that also match /api/local/getTodosMisProductos/
Apparently it got matched with /api/local/:id,
where you get req.params.id = "getTodosMisProductos" and got passed down to await Local.findById(req.params.id)
And mongoose can't convert "getTodosMisProductos" to ObjectId, hence the error.
The order in which you declare the route affects the matching priority.
The order is first comes first serves, so make sure you declare /api/local/addProducto or any other routes that starts with /api/local/ before declaring /api/local/:id

how to pass data from conttoller to route

I am trying to pass data from controller to route.
I want to change the status code from a controller to route. let say if in controller status 200 then change it into 400 from routes
or
just simple print hello or something from routes after the response
here is controller from controller file
contact controller. js
exports.index = function(req, res) {
Contact.get(function(err, contacts) {
if (err) {
res.json({
status: "error",
message: err
});
}
res.json({
status: "success",
message: "Contacts retrieved successfully",
data: contacts
});
});
};
here is the route of it from route file
contact router.js
var contactController = require('./contactController');
// Contact routes
router.route('/contacts')
.get(contactController.index)
Follow this article to design you application using express router
https://scotch.io/tutorials/learn-to-use-the-new-router-in-expressjs-4
define your controller like this
exports.index = function(req, res, next) {
Contact.get(function(err, contacts) {
if (err) {
next(null,{
status: "error",
message: err
});
}
next({
status: "success",
message: "Contacts retrieved successfully",
data: contacts
},null);
});
};
Define main app file like this
var contactController = require('./contactController');
var router = express.Router();
// apply the routes to our application
// route middleware that will happen on every request
router.use(function(req, res, next) {
// continue doing what we were doing and go to the route
next();
});
// about page route (http://localhost:8080/about)
router.get('/contacts', function(req, res) {
//here you can call your controller js method
contactController.index(req,res, function(data, err){
//change anything you want here and set into res.
if(err){
//change status and data
}
else{
//change status and data
}
})
});
Don't end the request-response cycle in the controller, just return the result from the controller instead of ending the cycle.
const httperror = require('http-errors');
exports.index = async function(parameter) {
Contact.get(function(err, contacts) {
if (err) {
throw new httperror(400, "Error occured!");
}
return {
status: "success",
message: "Contacts retrieved successfully",
data: contacts
}
});
};
request should start from the route and response should be sent from the route
const contactController = require('./contactController');
router.get('/contacts', function (req, res, next) {
contactController.index()
.then(result => {
res.json(result)
}).catch((error) => {
res.status(200).json({"Error":"Returned success code 200 even though error
occured"});
})
});

how to get the json of a route without fetch

l have a route which return me a json with user grant.
router.get('/test', function(req, res,next ) {
var token = req.cookies.auth;
if (!token) return res.status(401).send({ message: ""});
jwt.verify(token, config.secret, function(err, decoded) {
if (err) return res.status(500).send({ message: "Failed to authenticate token."});
User.findById(decoded.id,function(err,user){
if (err) return res.status(500).send({ message: "problmes found user, sorry "});
if(!user) return res.status(404).send({message: "user not found "});
res.status(200).send({message:user.role});
});
});
});
i want to catch the json response in a variable for another route to authorize an action.
router.get('/', function(req, res, next) {
if (jsonresponse == grant ){
var allMusique;
var idMaxMusique;
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db("projet_node");
dbo.collection("musiques").find({}).toArray(function(err, result) {
if (err) throw err;
allMusique = result;
var size = allMusique.length-1;
idMaxMusique = parseInt(result[size].id)+1;
res.render('musiques', { resultat: allMusique, idMax: idMaxMusique });
});
});}
else{
res.render('unauthorized');
}
});
create Controllers/middlewares/authenticate.js file and write the below code
const jwt = require('jsonwebtoken')
module.exports = (req, res, next) => {
const token = req.headers['token'] || req.body.token || req.cookies.token;
if (token) {
try {
req.decoded = jwt.verify(token, JWT_SECRET_KEY)
// JWT_SECRET_KEY -> config.secret
next()
} catch (err) {
res.status(403)
.send({ success: false, message: 'Failed to authenticate token.' })
}
}
return res.status(403).send({
success: false,
message: 'No token provided.'
})
}
create Controlller/UserController.js file
exports.getUser = function (req, res) {
// Perform Your requirement of code
// return Something
}
exports.getUserTest = function (req, res) {
// Perform Your requirement of code
// return Something
}
In Your routes/routes.js file
const authenticate = require('./Controllers/middlewares/authenticate');
const UserController = require('./Controllers/UserController');
// Routes with authentication. User must be login for get this routes
router.get('/getUser', authenticate, UserController.getUser);
// Routes without authentication. No need to login
router.post('/getUserTest', UserController.getUserTest);
Most Important require('PathOfFile') properly or Simply always double check path of file and folder in require.

why is my jwt token returning null on the backside but not the front

I am trying to make a GET request to my mLab database. I pass a JWT token with the request and logged it on both the client and server. It reads correctly on the client but shows null on the server. Any help would be much appreciated. I am using Node.js and Angular.
I am pretty new to this, so I apologize in advance if the mistake is obvious.
Here is the server's GET route:
router.get('/', (req, res, next) => {
var decoded = jwt.decode(req.query.token);
console.log(decoded);
console.log('employees');
if(decoded) {
return Company.find({_id: decoded._id})
.populate('user', 'firstName')
.exec(function(err, company) {
if (err) {
return res.status(500).json({
title: 'An error occurred',
error: err
});
}
res.status(200).json({
message: 'Success',
obj: company
});
});
} else {
return res.status(401).json({
title: 'Not authenticated',
error: {
message: 'Please create an account or sign in'
}
});
}
console.log(company);
});
Here is the client:
getEmployees() {
const token = localStorage.getItem('token')
? '?token=' + localStorage.getItem('token')
: '';
console.log(token);
return this.http.get('http://localhost:3000/company' + token)
.map((response: Response) => {
const employees = response.json().obj;
console.log(employees);
let transformedEmployees: Employee[] = [];
for (let employee of employees) {
transformedEmployees.push(new Employee(
employee.firstName,
employee.lastName,
employee.email,
employee.password,
employee.jobTitle,
employee.isAdmin,
employee.tasks
));
}
console.log(transformedEmployees)
this.employees = transformedEmployees;
return transformedEmployees;
})
.catch((error: Response) => {
this.errorService.handleError(error.json());
return Observable.throw(error.json())
});
}
You should NOT be placing your token in the Authorization header of your request.
You use an express middleware to decode the token:
const myDecoder = (req, res, next) => {
req.user = jwt.decode(req.headers.authorization);
next();
}
Then, you place in your route:
router.get('/', myDecoder, (req, res) => {
let user = req.user;
console.log(user);
console.log('employees');
if (user) { blah }
...
Should not be passing the entire token over the URL itself.

Resources