Recently i've been trying to rewrite my node.js express app to be more in line with the mvc principle. I've also been trying to incorporate mongoose. I'm having a problem with calling the static functions on a mongoose model.
userSchema.statics.findDuplicates = function (cb) {
console.log("Duplicates called");
this.findOne({ email: this.email }, function(err, result){
if (err) throw err;
if (result) {
cb("A user with this email has already been created.");
} else {
cb("");
}
});
}
Now the problem is that i'm later exporting a model using this schema, so this is all contained in one file:
module.exports = mongoose.model('User', userSchema);
When i later call this inside a controller, (obviously requiring and initiating the model beforehand):
user.findDuplicates(function(result){
if (result) {
res.send("Selle e-mailiga kasutaja on juba loodud.");
console.log("Duplicates");
} else {
user.save();
res.send("Kasutaja loodud.");
console.log("User created with password.")
}
});
It just never gets called. Node tells me it accepted a post, but got a 500 internal server error, and the "Duplicates called" inside findDuplicates does not appear in the console. Something is very wrong here, and i do not know how to fix it.
EDIT: Full controller code:
var express = require('express');
var router = express.Router();
var User = require("../models/user.js");
router.get('/', function(req, res, next) {
res.render('users',{title: "Lisa kasutaja"});
});
router.post('/', function(req, res, next) {
var query = req.body;
var message = "";
console.log("Post recieved " + JSON.stringify(query));
if (query.password != query.repeatPassword){
res.send("Paroolid ei ole võrdsed.");
console.log("Passwords don't match");
} else {
var user = new User({
firstName: query.firstName,
lastName: query.lastName,
telephone: query.telephone,
email: query.email,
password: query.password
});
console.log("User created");
user.findDuplicates(function(result){
if (result) {
res.send("Selle e-mailiga kasutaja on juba loodud.");
console.log("Duplicates");
} else {
user.save();
res.send("Kasutaja loodud.");
console.log("User created with password.")
}
});
}
});
module.exports = router;
Your problem resides in the fact that you're calling a static method in an instance of a model, which is not correct. See the difference below:
// if you define a static method
userSchema.statics.findDuplicates = function (cb) {
// do your stuff
}
// you call it this way
var User = require("../models/user.js");
User.findDuplicates(function (result) {
// do your stuff
});
// if you define an instance method
userSchema.methods.findDuplicates = function (cb) {
// do your stuff
};
// you call it this way (on an instance of your model)
var User = require("../models/user.js");
var user = new User({
firstName: query.firstName,
lastName: query.lastName,
telephone: query.telephone,
email: query.email,
password: query.password
});
user.findDuplicates(function (result) {
// do your stuff
});
Related
To clean up my code, I decided to put all of my schemas and routes into different files in my directory, and require them in my app.js. Each Schema corresponds to each route. For all but one of my routes, I have gotten this to work, but for one specific one, I cannot find out why I am getting it as undefined.
Here is the code I have in my app.js (the main file)
// Schemas
const Transaction = require ("./models/transaction");
User = require ("./models/user");
Ticket = require ("./models/ticket");
Job = require ("./models/job");
Client = require ("./models/client");
// Routes
require("./routes/users")(app);
require("./routes/tickets")(app);
require("./routes/clients")(app);
require("./routes/jobs")(app);
require("./routes/transactions")(app);
All of these work, except for my transaction route.
Here is its schema:
// =======================Transaction Schema "./models/transaction"
var transactionSchema = new mongoose.Schema({
job: String,
client: String,
deposited_by_user: String,
transaction_info:{
amount: mongoose.Decimal128,
method: String,
receipt_number: String,
date: {type: Date}
},
billing_address: {
street: String,
city: String,
state: String,
zip: String
},
notes: String,
date_added: {type: Date, default: Date.now}
});
module.exports = mongoose.model("Transaction", transactionSchema);
And its route:
module.exports = function(app) {
// =======================Transaction "./routes/transactions"
// index
app.get("/transactions", function(req, res){
Transaction.find({}, function(err, transactions){ // It is at this line where it is telling me that "Transaction" is undefined. However, with this code pasted into the app.js exactly the same as it is here, it works fine.
if(err){
console.log("error")
} else {
for (let i = 0; i < transactions.length; i++){
transactions[i]["transaction_info"]["new_amount"] = numberWithCommas(transactions[i]["transaction_info"]["amount"]);
}
res.render("transactions", {transactions: transactions});
}
});
});
// new
app.get("/transactions/add", async function(req, res){
let endCollections = [];
for (let i = 0; i < collections.length; i++){
await collections[i].find({}, function(err, foundCollection){
if (err) {
console.log(err);
} else {
endCollections[i] = foundCollection;
}
});
}
res.render("add_transaction", {users: endCollections[0], clients: endCollections[2], jobs: endCollections[3]});
});
// show
app.get("/transactions/:id", function(req, res){
Transaction.findById(req.params.id, function(err, foundTransaction){
if (err){
console.log(err);
} else {
// Redirect
let price = numberWithCommas(foundTransaction["transaction_info"]["amount"]);
res.render("transaction", {transaction: foundTransaction, price: price});
}
});
});
// edit
app.get("/transactions/:id/edit", function(req, res){
Transaction.findById(req.params.id, async function(err, foundTransaction){
if (err){
console.log("error")
} else {
let endCollections = [];
for (let i = 0; i < collections.length; i++){
await collections[i].find({}, function(err, foundCollection){
if (err) {
console.log(err);
} else {
endCollections[i] = foundCollection;
}
});
}
let deposit_date = foundTransaction["transaction_info"]["date"];
deposit_date = moment(deposit_date).format("MM-DD-YYYY");
res.render("edit_transaction", {transaction: foundTransaction, users: endCollections[0], clients: endCollections[2], jobs: endCollections[3], deposit_date: deposit_date});
}
});
});
// create
app.post("/transactions", function(req, res){
// Create Transaction
Transaction.create(req.body.transaction, function(err, newTransaction){
if (err){
console.log(err)
} else {
// Redirect
res.redirect("/transactions");
}
});
});
// update
app.put("/transactions/:id", function(req, res){
// Update transaction
Transaction.findByIdAndUpdate(req.params.id, req.body.transaction, function(err, updatedTransaction){
if (err){
console.log(err)
} else {
// Redirect
res.redirect("/transactions/" + req.params.id);
}
});
});
// delete
app.delete("/transactions/:id", function(req, res){
// Delete job
Job.findByIdAndRemove(req.params.id, function(err, deletedTransaction){
if (err){
console.log(err)
} else {
// Redirect
res.redirect("/transactions");
}
});
});
}
I do not believe the Schema is the problem because when I paste the Transaction route code directly into my app.js file, character for character, it works fine. However, with it split (and the code stays exactly the same in the transactions.js file) I am getting the error when I load the page that "Transaction is undefined" at the part of my code that starts with Transaction.find
Overall, I cannot understand why when the route is in the app.js file, it works just fine, but when it is in a separate file, the Transaction variable is considered undefined; this is all despite it being modeled exactely the same as my other routes and schemas. Is there something here I am not seeing? Thanks.
1)
Instead of:
app.get("/transactions", function(req, res){
.
.
app.get("/transactions/:id", function(req, res){
Maybe just:
app.get("/", function(req, res){
.
.
app.get("/:id", function(req, res){
and so on?
2)
Transaction.find({}, function(err, transactions){
Instead of {} try ()
Transaction.find((), function(err, transactions){
It looks as you are passing one empty object
I'm building a simple API with Express and Sequelize JS. I tried to update an record with req.School but nothing happend. I shared the code below. Also I got no error for output. Can you help me ?
Controller js
module.exports.params = function(req, res, next, id) {
School.find({
where: {
id: id
}
})
.then(function(school) {
if (!school) {
next(new Error("School not found by given ID"));
}else {
req.School = school;
next();
}
}, function(err) {
next(err);
});
};
module.exports.delete = function (req,res,next) {
req.School.Status = SysEnums.Deleted;
School.update(req.School,{
where:{
id:req.School.id
}
}).then(function (deleted) {
res.json(deleted);
});
};
Route JS
var router = require("express").Router();
var controller = require("../Controllers/SchoolController");
router.param("id",controller.params);
router.route("/").get(controller.get);
router.route("/:id").get(controller.getOne).delete(controller.delete);
module.exports = router;
Since the params method, req.School is a sequelize object. Here is the params method.
module.exports.params = function(req, res, next, id) {
School.find({
where: {
id: id
}
})
.then(function(school) {
if (!school) {
next(new Error("School not found by given ID"));
}else {
req.School = school;
next();
}
}, function(err) {
next(err);
});
};
As #Shivam said it' s a sequelize object, I could use provided methods. I want to soft delete my record, so here is my delete method with Sequelize JS.
module.exports.delete = function(req, res, next) {
try {
req.School.Status = SysEnums.Deleted;
req.School.save().then(function(entity) {
res.json(entity);
});
} catch (e) {
console.log(e);
}
};
#PandhiBhaumik
Thank you for your help. I' ll try to use build method as a second option.
I am creating online course application and I want only authenticated users to see the course details and course lecture. I am using local strategy of passport authentication for user authentication. I added isAuthenticated in my routes, however, still the unauthenticated users can view the video lectures.
Here is my routes file. file name:- courses.server.routes.js
'use strict';
/**
* Module dependencies
*/
var coursesPolicy = require('../policies/courses.server.policy'),
courses = require('../controllers/courses.server.controller');
var passport = require('passport');
var isAuthenticated = function(req, res, next) {
// if user is authenticated in the session, call the next() to call the next request handler
// Passport adds this method to request object. A middleware is allowed to add properties to
// request and response objects
if (req.isAuthenticated())
return next();
// if the user is not authenticated then redirect the user to the login page
res.redirect('/');
};
module.exports = function (app) {
// Courses collection routes
app.route('/api/courses').all(coursesPolicy.isAllowed)
.get(courses.list)
.post(courses.create);
// Single course routes
app.route('/api/courses/:courseId', isAuthenticated).all(coursesPolicy.isAllowed)
.get(courses.read)
.put(courses.update)
.delete(courses.delete);
// Finish by binding the course middleware
app.param('courseId', courses.courseByID);
};
Here is my route controller file. file name:- courses.server.controller.js
'use strict';
/**
* Module dependencies
*/
var path = require('path'),
mongoose = require('mongoose'),
Course = mongoose.model('Course'),
errorHandler = require(path.resolve('./modules/core/server/controllers/errors.server.controller'));
var passport = require('passport');
/**
* Create an course
*/
exports.create = function (req, res) {
var course = new Course(req.body);
course.user = req.user;
course.save(function (err) {
if (err) {
return res.status(422).send({
message: errorHandler.getErrorMessage(err)
});
} else {
res.json(course);
}
});
};
/**
* Show the current course
*/
exports.read = function (req, res) {
// convert mongoose document to JSON
var course = req.course ? req.course.toJSON() : {};
// Add a custom field to the Course, for determining if the current User is the "owner".
// NOTE: This field is NOT persisted to the database, since it doesn't exist in the Course model.
course.isCurrentUserOwner = !!(req.user && course.user && course.user._id.toString() === req.user._id.toString());
console.log('course value is: ' + course);
console.log('video lecture embed value is: ' + course.courseLecture.lecture_video);
res.json(course);
};
/**
* Update an course
*/
exports.update = function (req, res) {
var course = req.course;
course.title = req.body.title;
course.content = req.body.content;
course.courseLecture.lecture_video = req.body.courseLecture.lecture_video;
console.log('course lecture video url is: ' + req.body.courseLecture.lecture_video);
course.save(function (err) {
if (err) {
return res.status(422).send({
message: errorHandler.getErrorMessage(err)
});
} else {
res.json(course);
}
});
};
/**
* Delete an course
*/
exports.delete = function (req, res) {
var course = req.course;
course.remove(function (err) {
if (err) {
return res.status(422).send({
message: errorHandler.getErrorMessage(err)
});
} else {
res.json(course);
}
});
};
/**
* List of Courses
*/
exports.list = function (req, res) {
Course.find().sort('-created').populate('user', 'displayName').exec(function (err, courses) {
if (err) {
return res.status(422).send({
message: errorHandler.getErrorMessage(err)
});
} else {
res.json(courses);
}
});
};
/**
* Course middleware
*/
exports.courseByID = function (req, res, next, id) {
if (!mongoose.Types.ObjectId.isValid(id)) {
return res.status(400).send({
message: 'Course is invalid'
});
}
Course.findById(id).populate('user', 'displayName').exec(function (err, course) {
if (err) {
return next(err);
} else if (!course) {
return res.status(404).send({
message: 'No course with that identifier has been found'
});
}
req.course = course;
next();
});
};
I'm not able to figure out, whats going wrong here.
Call isAuthenticated function from http verb means from get, post, patch, delete
like:
app.route('/api/courses/:courseId')
.get(isAuthenticated, courses.read)
also isAuthenticated can be write in another file and use it from your route
Can see this example
I'm making a simple webapp with facebook login.
If the facebook login button on my page is clicked,
FB.api(
'/me',
'GET',
{"fields":"id,name,birthday,gender"},
function(response) {
$.post('fb_login', response, "json");
}
);
is called, and a router handles '/fb_login' request; in the router the server checks the id of json object is already in its DB. If not, res.render('signup', ...) should be called.
However it didn't work. I already checked that res.render() was called, but the page 'signup.jade' didn't show up.
Here is my source code of router.
var express = require('express');
var router = express.Router();
var mysql = require('mysql');
var pool = mysql.createPool({
host: 'localhost',
user: 'root',
password: '1012'
});
/* GET home page. */
router.post('/', function(req, res, next) {
var userid = req.body.id;
if (userid) {
pool.getConnection(function(err, connection) {
if (err) {
console.error('DB Connection error!!');
return;
}
console.log('DB Connection Success!!');
connection.query('use vasket');
connection.query('select count(*) result from user where userID=?',
[userid], function(err, result, field) {
var isAlreadyUser = result[0].result;
console.log(isAlreadyUser);
if (isAlreadyUser == 1) {
req.session.userid = userid;
res.redirect('/');
res.end();
console.log('DB FB Login Success!!');
connection.release();
}
else {
connection.release();
console.log('FIRST TIME!');
//This method was called, but the page rendered didn't
res.render('signup', {id: req.body.id, name: req.body.name, birthday: req.body.birthday, gender: req.body.gender});
}
});
});
} else {
res.redirect('/');
res.end();
}
How can I fix it?
To help debugging maybe you can modify your code like that :
// ... your code before
else {
connection.release();
console.log('FIRST TIME!');
console.log(req.body);
//This method was called, but the page rendered didn't
res.render(
'signup',
{
id : req.body.id,
name : req.body.name,
birthday: req.body.birthday,
gender : req.body.gender
} ,
function(err, html){
if(err) console.log(err);
console.log(html);
//res.send(html);
// trying same but forcing status
res.status(200).send(html);
}
);
}
});
});
} else {
res.redirect('/');
res.end();
}
This is an older question, but it's still in need of a solid answer. I had the exact same problem, but I think I've figured it out.
If the server's returning the proper response, that's not where your problem lies. jQuery (in the browser) will render the response, but you have to tell it to do so.
Here's what I had that was responding properly but not rendering:
$("#getsome").click(function() {
$.get("/task/create");
});
And here's how I got it to render:
$("#getsome").click(function() {
$.get("/task/create", function( data ) {
document.write(data);
});
});
Note that you may not need to replace the entire DOM like I'm doing.
References:
Replacing the entire
DOM
jQuery.get
I am trying to inject a session value into the request so i can use it on different situation on my app. What i am doing is calling a function by giving the id to search for a user into database and return me the name of that specific user. The issue i am facing is when i try to declare the session, it looks like is not working or the callback is not letting this new value out.
Let me show you my code example for an better idea:
The middleware
var express = require('express');
var session = require('express-session');
var router = express.Router();
var userSession = require('../../helpers/user/userSession');
router.use(function(req, res, next){
if (req.method == "GET") {
if (!req.user) {
req.session.username = '';
}else{
var sess = userSession.findUser(req.user, function(err, user){
if (user) {
console.log(user); //It contains the value i need
req.session.username = user; // Supposed to inject the user value to the username session variable.
};
console.log(req.session.username); //it works until here, out of this function not anymore.
});
console.log(req.session.username); //the req.session.username is empty now
};
return next();
}else{
return next();
}
});
Check if user exist
var mongoose = require('mongoose');
var User = mongoose.model('database')
module.exports = {
findUser: function(user, callback){
User.findOne({ 'unq_id' : user }, function(err, user){
if (err) {
console.log('Error: ' +err);
return callback(err, false);
};
if (user) {
//console.log(user);
return callback(null, user.user_collection.firstname);
}else{
return callback(err, false);
};
});
}
}
One idea is to give to that sess variable the value of user, but it appears very difficult since is asynchronous call. I am sure some of might have run into this issue.
How can i get around this? any suggestion will be much appreciated.
How about this?
router.use(function(req, res, next){
if (req.method == "GET") {
if (!req.user) {
req.session.username = '';
next();
} else {
userSession.findUser(req.user, function(err, user){
if (user) {
req.session.username = user;
};
next();
});
}
} else {
next();
}
});
That way it won't go to the next middleware until after the username has been retrieved.