I am using Mongoose and Express.js .
My Post request looks like this :
$('#update').on('click', function() {
var uname = $('#username').val();
var name = $('#name').val();
var json = {
user_name: name,
user_username: uname,
};
$.post("/saveprofile", json, function(data, error, xhr) {
if(!err)
window.location.href = "/feed";
});
});
This is how I'm handling the request :
app.post('/saveprofile', isLoggedIn, function(req, res) {
var username = req.body.user_username;
var uname = req.body.user_name;
User.findOne({$or:[{"facebook.email":req.user.facebook.email},{"local.email":req.user.local.email}]}, function (err, user){
user.name = uname;
user.username=username;
user.save();
res.send({redirect: '/feed'});
})
});
My documents get added into the db successfully but what I want is, when that happens it should redirect to a new page /feed . The above snippets mention 2 methods I've tried. But I've got no result . It doesn't redirect !
Edit:
Testing with exception handling gives undefined
app.post('/saveprofile', isLoggedIn, function(req, res) {
var username = req.body.user_username;
var uname = req.body.user_name;
try{
User.findOne({$or:[{"facebook.email":req.user.facebook.email},{"local.email":req.user.local.email}]}, function (err, user){
user.name = uname;
user.username=username;
user.save();
res.redirect("http://stackoverflow.com")
});
}
catch(ex){
console.log(ex) ;
}
});
Edit 2 :
I tried a sample request ...even it doesn't seem to redirect !
app.post('/test', isLoggedIn, function(req,res){
res.redirect("http://www.stackoverflow.com")
})
You can't make a redirection after an AJAX. You need to do it with Javascript.
So here is how we can do it,
Post request :
$('#update').on('click', function() {
var uname = $('#username').val();
var name = $('#name').val();
var json = {
user_name: name,
user_username: uname,
};
$.post("/saveprofile", json, function(data, err, xhr) {
if(!err)
window.location.href = data.redirect;
});
});
Server
app.post('/saveprofile', isLoggedIn, function(req, res) {
var username = req.body.user_username;
var uname = req.body.user_name;
User.findOne({$or:[{"facebook.email":req.user.facebook.email},{"local.email":req.user.local.email}]}, function (err, user){
user.name = uname;
user.username=username;
user.save(function(err){
if(!err)
res.send({redirect: '/feed'});
});
})
});
Link to Original Answer
Try res.redirect() instead :
res.redirect("/feed");
Related
I am working on a model here:
// user.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var bcrypt = require('bcrypt');
// Define collection and schema for Users
let User = new Schema(
{
firstName: String,
lastName: String,
emailaddress: String,
password: String,
},
{
collection: 'users'
}
);
// authenticate input against database documents
User.statics.authenticate = ((emailaddress, password, callback) => {
User.findOne({ emailaddress: emailaddress })
.exec(function(error, user){
if(error){
return callback(error)
} else if (!user){
console.log('User not found!');
}
bycrypt.compare(password, user.password, function(err, result){
if(result === true){
return callback(null, user);
} else {
return callback();
}
})
})
});
module.exports = mongoose.model('User', User);
As you can see on my model I put the User.statics.authenticate on my codes to do some authentication. And then on my login.js route file:
const path = require('path');
const express = require('express');
const router = express.Router();
const db = require('../../database/index');
const axios = require('axios');
const User = require('../../database/models/user');
router.get('/', (req, res) => {
console.log('hi there this is working login get');
});
router.post('/', (req, res) => {
var emailaddress = req.body.emailaddress;
var password = req.body.password;
if( emailaddress && password ){
User.authenticate(emailaddress, password, function(err, user){
if(err || !user){
console.log('Wrong email or password!');
} else {
req.session.userId = user._id;
return res.redirect('/');
}
});
} else {
console.log('both fields are required...');
}
});
module.exports = router;
I called the function and then User.authenticate function and also I created the route for root w/c is the sample that I want to protect and redirect the user after login:
router.get('/', (req, res) => {
if(! req.session.userId ){
console.log('You are not authorized to view this page!');
}
User.findById(req.session.userId)
.exect((err, user) => {
if(err){
console.log(err)
} else {
res.redirect('/');
}
})
});
Upon clicking submit on my react form it returns this error:
TypeError: User.findOne is not a function
at Function.User.statics.authenticate (/Users/mac/Documents/monkeys/database/models/user.js:35:8)
I checked the Mongoose documentation and it seems I am using the right syntax.Any idea what am I doing wrong here? Please help! Sorry super beginner here!
PS. I've already installed and set up the basic express session too.
UPDATES:
I remove the arrow function from my call and use this.model.findOne but still get the typerror findOne is not a function
// authenticate input against database documents
User.statics.authenticate = function(emailaddress, password, callback){
this.model.findOne({ emailaddress: emailaddress })
.exec(function(error, user){
if(error){
return callback(error)
} else if (!user){
console.log('User not found!');
}
bycrypt.compare(password, user.password, function(err, result){
if(result === true){
return callback(null, user);
} else {
return callback();
}
})
})
};
findOne is a method on your User model, not your user model instance. It provides its async results to the caller via callback:
User.findOne({field:'value'}, function(err, doc) { ... });
I need to find the events of a user but I can't retrieve the user since I can't pass the user id to route file of events.
app.js :
var Router = express.Router();
Router.use("/users/:uid/events", events);
events.js :
Router.post("/",function (req,res) {
User.findOne({
_id:req.params.uid
},function (err, foundUser) {
if(err){
res.json({success:false, message:"The user couldn't be found"});
}else{
console.log(req.params.uid);
console.log(foundUser);
var event = new Event();
event.title = req.body.title;
event.description = req.body.description;
event.save(function (err) {
if(err){
console.log(err);
}
});
foundUser.events.push(event);
}
})
});
As a result :
uid = undefined
foundUser = null
How can i pass uid parameter to the events.js?
I think this should work.
app.js:
var Router = express.Router();
Router.use("/users/", events);
events.js:
Router.post("/:uid/events", function (req,res) {
User.findOne({
_id:req.params.uid
},function (err, foundUser) {
if(err){
res.json({success:false, message:"The user couldn't be found"});
}else{
console.log(req.params.uid);
console.log(foundUser);
var event = new Event();
event.title = req.body.title;
event.description = req.body.description;
event.save(function (err) {
if(err){
console.log(err);
}
});
foundUser.events.push(event);
}
});
});
While Lazyexpert's answer is a way to solve this problem, I found what I have been exactly looking for today.
In order to pass uid from app.js
Router.use("/users/:uid/events", events);
to another route make sure that your router contains {mergeParams:true}
var express = require('express');
var Router = express.Router({mergeParams:true});
I am writing a website in node.js and express. At the current moment if i run the website the login controller is loaded every time and it just loads forever.
I don't want the login controller to be loaded when i load the page. I need to to be loaded when i click on my button login.
This is my Router.js
module.exports = function(app) {
app.get('/login/:username/:password', function(req, res) {
var login = require('../controllers/login');
login.userLogin
});
app.get('/', function(req, res, next) {
res.render('index', {
title: 'Trace User Login'
});
});
}
This is my jade page
extends layout
block content
div#login
label#lblInfo Please enter your username and password.
input#txtUsername.form-control.login(placeholder='Username', type='text')
input#txtPassword.form-control.login(placeholder='Password', type='password')
a#btnLogin.btn.btn-danger.btn-lg(href='#') Login
block endContent
div.footer
p#dev Developed by :
a.redtextheadingsmall(href="http://www.verishare.co.za/", target="_blank") VeriShare
p Telephone: +27 (18) 294 1000 Fax: +27 (18) 294 3880 Email:
a.redtextheadingsmall(href="mailto:tracesupport#vccb.co.za") tracesupport#vccb.co.za
p We recommend that you view this website with: MS IE 10+ or Google Chrome 20.0+ or Mozilla 20.0+
p#copy Copyright © 2014 .. All Rights Reserved.
How do i go about getting the login module to only load once i click on the login button. At the current moment the page is not even rendering as it is just stuck in loading my login Controller
console.log('Inside controller');
exports.userLogin = function(request, respond){
var Connection = require('tedious').Connection;
var Request = require('tedious').Request;
var config = {
userName: 'username',
password: 'password',
server: 'localhost',
options: {
instanceName: 'instance',
database: 'tempdb'
}
};
var connection = new Connection(config);
connection.on('connect', function(err) {
if (err)
console.log(err);
else
var userName = request.params.username;
var password = request.params.password;
loginRequest(userName,password);
});
};
function loginRequest(Username,Password) {
request = new Request("sp_WT_ValidateUser", function(err, rowCount) {
if (err) {
console.log(err);
} else {
console.log(rowCount + ' rows');
}
});
request.addParameter('UserName', TYPES.VarChar, Username);
request.addParameter('Password', TYPES.VarChar, Password);
request.addOutputParameter('InvalidPasswordCounter', TYPES.VarChar);
request.on('row', function(columns) {
columns.forEach(function(column) {
console.dir(column);
});
console.log('');
});
connection.execSql(request);
}
This seemed to fix my problems
var router = require('express').Router();
var login = require('../controllers/login');
router.post('/login', login.userLogin);
router.get('/', function(req, res, next) {
res.render('index', {
title: 'Trace User Login'
});
});
module.exports = router;
I got a 500 error and de description says: Mongoose is not defined when i call /delPost, but in the same file "post.js" the functions /addPost and /getPosts are working and use mongoose.model without problems. im a newbie and have searched a lot but can't fix that issue, hope you can help me.
routes/post.js
var express = require('express');
var mongoose = require('mongoose');
var router = express.Router();
POST message to board (WORKING)
router.post('/addPost', function(req, res) {
if(req.body){
var post = mongoose.model('post');
var newpost = new post(req.body);
console.log(newpost);
newpost.save(function(err, newpost){
if(err) return res.send(200,{msg: 'not saved'});
})
console.log('save ok');
}
else{
res.send(200,{msg: 'no data received'});
}
});
GET messages from DB to show in HTML (WORKING)
router.get('/getPosts', function(req, res) {
var post = mongoose.model('post');
post.find(function(err, posts){
if(err) return console.log(err);
// console.log(posts);
res.send(posts);
})
});
DELETE a post (NOT WORKING)
router.post('/delPost/:rel', function(req, res){
// var mongoose = require('mongoose');
var post = moongose.model('post');
post.remove({_id: req.params.rel},function(err) {
if(err) return handleError(err);
console.log(err);
})
});
AJAX Call to /delPost route
function delPost(event){
event.preventDefault();
console.log('delpost');
var confirmation = confirm('Are you sure to delete this message?');
if(confirmation === true){
$.ajax({
type:'POST',
url: '/post/delPost/' + $(this).attr('rel')
}).done(function(response){
console.log('delete done.. ');
}).fail(function(response){
console.log('delete failed');
})
}
};
Building an API with node and express. In my "home" route i set a session with a users id.
When i want to add and update information on a user i want to access the session to know which user to update. In my get routes i can access the session, but in my route with put method its always undefined. Why is this?
app.get('/users/:id/spots', spot.findSpotsByUserId); //I set the session in this method
app.get('/spots/:id', spot.findById);
app.put('/userspot/spot/:spotId/add'', spot.addUserSpot);
exports.findSpotsByUserId = function(req, res) {
var id = req.params.id; //Should ofc be done with login function later
db.collection('users', function(err, collection) {
collection.findOne({'_id':new BSON.ObjectID(id)}, function(err, user) {
if (err) {
res.send({'error':'Couldnt find user'});
} else {
req.session.userId = id;//<----- sets session
console.log("SESSION",req.session.userId);
}
......}
exports.findById = function(req, res) {
var id = req.params.id;
console.log('Get spot: ' + id);
console.log("SESSION!",req.session.userId);// <----prints the id!
db.collection('spots', function(err, collection) {
collection.findOne({'_id':new BSON.ObjectID(id)}, function(err, item) {
res.send(item);
});
});
};
exports.addUserSpot = function(req, res) {
var user = req.session.userId;
var spot = req.params.spotId;
console.log("SESSION!",req.session.userId);// always UNDEFINED!
//........}
You are looking for req.params.userId, not req.session.
The session is persisted between multiple calls, and it has no connection to the params object. You can set req.session.userId in a previous call and access it here, but I don't think this is what you want.
Try this:
exports.findById = function(req, res) {
req.session.test = "from findById";
...
};
exports.addUserSpot = function(req, res) {
console.log(req.session.test, req.params.userId);
...
};