When i'm getting to my routes and requesting to getUSers from my mongoDB it says the User.find() is not defined. Sorry in advance if i use incorrect terminology i'm jumping in face first.
I'm assuming my routing is done incorrectly somewhere or i didn't include one file somewhere I'm hoping ya'll can help me determine if i'm either storing a file incorrectly in my structure, calling a file at the wrong time, or not initializing a variable correctly? Thanks for the help in advance.
Do i need an additional plugin to read and write to the database?
I keep getting the the following error it says usercontroller.js:20
Error: <!DOCTYPE html><html><head><title></title><link rel="stylesheet" href="/stylesheets/style.css"></head><body><h1>undefined is not a function</h1><h2></h2><pre>TypeError: undefined is not a function
at getUsers (c:\Users\Ravenous\kitchen\routes\user_api.js:5:10)
at c:\Users\Ravenous\kitchen\routes\user_api.js:26:3
file structure in case it helps
-bin
-node_modules-\
-user-app-\
-user.js //this is the model written with mongoose schema
-public-\
-html
-images
-javascripts-\
-userController
-userService //AngularJS factory
-stylesheets
-index.html
-routes-\
-user_api.js //with other useful routes inside.
-views //has some jade view engines that came with express-generator
-app.js
-package.JSON
I have this for the user model -- file: node_modules/user-app/user.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var UserSchema = new Schema({
user_name: String,
password: String,
email: String,
location: String,
date_created : {type: Date, default: Date.now}
});
var User = mongoose.model('User', UserSchema);
module.exports= {User:User};
I then import user.js file to the ./routes/user_api.js file which looks like this
user_api.js :
var User = require('./node_modules/user-app/user');
function getUsers(res){
User.find().populate('users').exec(function(err, users){
// if there is an error retrieving, send the error. nothing after res.send(err) will execute
if (err){
res.send(err)
}else{
console.log(users[0][0].name)
res.json(users); // return all users in JSON format
db.close();
}
})
};
module.exports = function(app) {
// api -----------------------------------------------------------
// get all users
app.get('/node_modules/user-app/user', function(req, res) {
// use mongoose to get all users in the database
getUsers(res);
});
// create user and send back all users after creation
app.post('/node_modules/user-app/user', function(req, res) {
// create a user, information comes from AJAX request from Angular
User.create({
user_name: User.user_name,
password: User.password,
email: User.email,
location: User.location,
done : false
}, function(err, todo) {
if (err){
res.send(err);
}else{
// get and return all the users after creating one
getUsers(res);
}
});
});
// delete a user
app.delete('/node_modules/user-app/user:user_id', function(req, res) {
User.remove({
_id : req.params.user_id
}, function(err, user) {
if (err)
res.send(err);
getUsers(res);
});
});
};
I'm adding this user_api route and get method to the app.js file like so
// loading some required modules above this
var app = express();
var user = require('./routes/user_api')(app);
to handle the Angular and Node connection and state sharing i'm using the following
userController.js:
(function(){
'use strict'
var userCtrl = angular.module("userController",[]);
console.log('userController init')
userCtrl.controller('UserController', ['$scope','$http','Users',function($scope,$http,Users){
$scope.users = {};
$scope.loading = true;
console.log ("user init");
Users.get()
.success(function(data){
$scope.users = data;
$scope.loading = false;
console.log(data)
})
.error(function(err){
console.log('Error: ' + err);
});
$scope.createUser = function(){
if($scope.users.user_name != false){
console.log($scope.users.user_name)
$scope.loading = true;
Users.create($scope.users)
.success(function(data){
$scope.loading=false;
$scope.users = data;
console.log(data);
})
.error(function(err){
console.log('Error: ' + err);
});
}
};
}]);
})();
Change the line where you require User to
var User = require('./node_modules/user-app/user').User;
This is because in your export statement when you define a user you have the line:
module.exports = {User:User};
So, you are exporting an object that has a User property on it.
Related
I recently move in NodeJs from PHP and developing a project in MEANJS. But i unable to use a controller method in other controller. I think that i have followed wrong pattern.
student.model.js
-----------------
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var studentSchema = new Schema({
....
....
})
student.controller.js
---------------------
var Student = require('../models/student.model');
module.exports.get = function(req, res, next) {
var where={};
if(req.body.hasOwnproperty('id')){
where._id=req.body.id;
}
....
....
Student.find(where)
.populate({ path: 'createdBy', select: '_id name' })
.exec(function(err, studentData) {
if (err)
return res.status(500).end(err.message);
res.status(200).json(studentData);
})
}
report.controller.js
---------------------
var StudentCtrl = require('../controller/student.controller');
module.exports.getStudentGrade = function(req, res, next) {
//HERE I WANT get StudentCtrl.get data then response to client
//somethings like----------------
StudentCtrl.get(req,res,next){
//get studentData here
//manipulation on studentData
res.status(200).json(myReportData);
}
//-------------------
}
Make 2 versions on student.controller.get, for example student.controller.getCallback which receives a callback which you can then use in report.controller.js
student.controller.js
---------------------
var Student = require('../models/student.model');
module.exports.get = function(req, res, next) {
var where={};
if(req.body.hasOwnproperty('id')){
where._id=req.body.id;
}
....
....
Student.find(where)
.populate({ path: 'createdBy', select: '_id name' })
.exec(function(err, studentData) {
if (err)
return res.status(500).end(err.message);
res.status(200).json(studentData);
})
}
module.exports.getCallback = function(req, callback) {
var where={};
if(req.body.hasOwnproperty('id')){
where._id=req.body.id;
}
....
....
Student.find(where)
.populate({ path: 'createdBy', select: '_id name' })
.exec(callback);
}
report.controller.js
---------------------
var StudentCtrl = require('../controller/student.controller');
module.exports.getStudentGrade = function(req, res, next) {
//HERE I WANT get StudentCtrl.get data then response to client
//somethings like----------------
StudentCtrl.getCallback(req, function(err, myReportData) {
//get studentData here
//manipulation on studentData
if (err)
return res.status(500).end(err.message);
res.status(200).json(myReportData);
});
}
MEAN Stack is a complete JavaScript framework, it is a mixed of Mango dB, Express JS, Angular JS and Node JS. here Node JS play the role of server-side programming and npm server, so if you are re-forming the framework it makes into you have to independently connect MangodB, Express JS, Angular JS to PHP. please refer the link http://meanjs.org/
Please follow these steps:
mongo
index.js
models
user.js
client.js
controllers
user.js
client.js
Now
in models put all the schemas
and in controller call relevant schemas and function like save() update etc
and in the last all the controller method access it in the index hs
I am getting the below error which doing a app in Node.js using express.
I am using Mongoose for my DB operations below i have detailed my design
Party.js
var mongoose = require("mongoose");
var partySchema = new mongoose.Schema({
partyName: String,
songs: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "Song"
}
]
});
module.exports = mongoose.model("Party", partySchema);
Song.js
var mongoose = require("mongoose");
var songsSchema = new mongoose.Schema({
videoId: String,
videoName: String
});
module.exports = mongoose.model("Song", songsSchema);
My App.js
app.post("/search/addSong", function(req, res) {
Party.find({partyName:"hello"},function(err,party){
if(err){
console.log("Failed in find");
} else {
console.log(party);
// console.log(typeof(req.body.videoId));
var videoId = req.body.videoId;
var newSong = [ {
videoId:req.body.videoId,
videoName:req.body.videoName
}
];
Song.create(newSong, function(err, createdSong){
if(err){
console.log("Error creating a new party");
} else {
console.log(createdSong);
party.songs.push(createdSong);// ERROR ON THIS LINE
party.save();
res.redirect("/search");
}
});
}
});
res.render("addSong");
});
I am able to create collection objects of the Party and Song individually, when I add the song to the party queue, I get the following error:
TypeError: Cannot read property 'push' of undefined
Can anyone please let me know what I a missing in here..!!
Thanks in advance.!!
find returns a cursor which is converted in an array. if you are expecting a single doc, call findOne.
app.post("/search/addSong", function(req, res) {
Party.findOne({partyName:"hello"},function(err,party){
if(err){
console.log("Failed in find");
} else {
console.log(party);
// console.log(typeof(req.body.videoId));
var videoId = req.body.videoId;
var newSong = [ {
videoId:req.body.videoId,
videoName:req.body.videoName
}
];
Song.create(newSong, function(err, createdSong){
if(err){
console.log("Error creating a new party");
} else {
console.log(createdSong);
party.songs.push(createdSong);// ERROR ON THIS LINE
party.save(function(err){
res.redirect("/search");
});
}
});
}
});
res.render("addSong");
});
Also put redirect in the callback of save, it will make sure that redirect is called only when save is completed, otherwise it would redirect without saving, giving the asynchronous nature of javascript.
I am trying to save a data in MongoDB with Mongoose with Express.JS 4 and Bluebird.
What I have done is like this-
bin/www
var mongoose = require('mongoose');
mongoose.Promise = require('bluebird');
.......
.......
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function()
{// successfully connected!
console.log("Successfully Connected to Mongo-DB");
});
And getting this in console-
Successfully Connected to Mongo-DB` - So, MongoDB connected successfully
models/post.js
var mongoose = require('mongoose');
var postSchema = new mongoose.Schema({
created_by: String, //should be changed to ObjectId, ref "User"
created_at: {type: Date, default: Date.now},
text: String
});
module.exports = mongoose.model('Post', postSchema);
app.js
var Post_Data = require("./models/post");
....
....
router.get('/', function(req, res, next)
{
var Post = mongoose.model("Post");
var post = new Post({
created_by: ""+Math.random()
});
console.log( Post.create(post) );
res.render(
'index',
{
title : 'Express',
site_name : 'Our Site',
layout : 'templates/layout'
}
);
});
And after that I am getting this in console-
Promise {
_bitField: 0,
_fulfillmentHandler0: undefined,
_rejectionHandler0: undefined,
_promise0: undefined,
_receiver0: undefined }
But, nothing is saved, a proof for that is -
I am finding this-
After using MongoBooster.
Update-
My DB config is like this-
"MONGO_URI": "mongodb://localhost:27017/express_test",
"MONGO_OPTIONS": {
"db": { "safe": true },
"name":"express_test"
}
So, can anyone please help, why it is not saving anything?
Thanks in advance for helping.
The .create() function is a shortcut for new Model and .save(). You are trying to .create an instance of Model rather than a simple Object. See Constructing documents in Mongoose's Models documentation for their quick example.
The return from a Mongoose data function is just the promise of an asynchronous task to be run in the future, logging that is largely pointless. Use .then() to wait until the promise has been resolved.
Error handling is missing from your code as well, something could be getting thrown there. Use a .catch() for promise error handling.
Post.create({ created_by: ""+Math.random() })
.then(function (result) {
console.log('Saved' result)
})
.catch(function (err) {
console.error('Oh No', err)
})
All of this can be done with callbacks (like the Mongoose docco examples) but promises, particularly bluebird promises are nicer.
I just use this syntax combination to create and save my model:
var myPage = new LandingPage({
user:req.user,
slug: req.body.slug,
}).save(function(err,savedModel){
if(!err){
console.log(savedModel);
}
});
You are calling the wrong model in your app.js module as you are importing the model as
var Post_Data = require("./models/post"); // <-- Post_Data model never used
....
....
but creating a new Post model instance in your router implementation as
var Post = mongoose.model("Post"); // <-- different model
var post = new Post({
created_by: ""+Math.random()
});
You need to call and use the correct models. So I would suggest you re-write your app.js module to use the save() method as:
var Post = require("./models/post"); // <-- import correct Post model
....
....
router.get('/', function(req, res, next) {
var post = new Post({ created_by: ""+Math.random() });
post.save().then(function(post) {
console.log(post); // <-- newly created post
res.render('index', {
title: 'Express',
site_name: 'Our Site',
layout: 'templates/layout'
});
})
.catch(function(err) {
console.error('Oopsy', err);
});
});
if you store post schema in a variable by require then can use that variable.
var Post_Data = require("./models/post");
so can use new Post_Data no need to use var Post = mongoose.model("Post"); because you have already exported this schema module.exports = mongoose.model('Post', postSchema);
you can try this one :
var Post_Data = require("./models/post");
router.get('/', function(req, res, next)
{
var post = new Post_Data({created_by: ""+Math.random()});
post.save(function(error, data) {
if(error) {
return res.status(500).send({error: 'Error occurred during create post'});
}
return res.render('index',{
title : 'Express',
site_name : 'Our Site',
layout : 'templates/layout'
});
});
});
So it's true that if you're creating a document in memory by calling new Post(values) that you will save it with post.save(cb); rather than 'Post.create(post);, but I'm thinking that the underlying issue (though this isn't easy to be certain of based on the code you're showing) is that you're connecting with the MongoDB driver, rather than mongoose itself. Yourdb` variable isn't shown to be declared in the code you posted, so I'm making it an assumption.
That said, if I'm right, you need to call mongoose.connect or mongoose.createConnection in order for Mongoose to know it's connected to the db and save documents to it. You can pass an existing connection to mongoose, so if you're already doing so then I apologize for my erroneous assumption.
I am moving from mongodb to Couchbase using Node.js. I want to perform the CRUD operations. Insert(create) and Get are working fine, but when I want to perform Update and Delete getting some error messages (Here update purpose using 'upsert','replace' are used) like:
TypeError: Cannot read property 'replace' of undefined
Here is code:
db.js
// Instantiate Couchbase and Ottoman
var couchbase=require('couchbase');
var ottoman=require('ottoman');
// Build my cluster object and open a new cluster
var myCluster = new couchbase.Cluster('localhost:8091');
var myBucket = myCluster.openBucket('default');
ottoman.bucket=myBucket;
require('./model/user');
ottoman.ensureIndices(function(){});
user.js
var db = require('./../db.js').myBucket;
var ottoman = require('ottoman');
var userMdl = ottoman.model('User', {
firstName: {type:'string'},
lastName: {type:'string'},
created: {type: 'Date', default:function(){return new Date()}},
email:'string',
phone: 'string'
},{
index: {
findByID: {
by: '_id'
},
}
})
module.exports = userMdl;
routes.js
var bodyParser = require('body-parser');
var db = require('../schema/db').myBucket;
var user=require('../schema/model/user');
var jsonParser = bodyParser.json();
var urlencodedParser = bodyParser.urlencoded({ extended: false });
module.exports = function (app) {
// Delete a record
app.post("/api/delete/:_id", function(req, res) {
console.log("_id:"+req.params._id)
if(!req.params._id) {
return res.status(400).send({"status": "error", "message": "A document id is required"});
}
db.delete({_id:req.params._id}, function(error, result) {
if(error) {
return res.status(400).send(error);
}
res.send(result);
});
});
app.post('/api/user/update/:id',function(req,res){
db.replace(req.params.id,{firstName:"Mahesh"},function(err,result){
if (err) {
res.status = 400;
res.send(err);
return;
}
else {
res.status = 202;
res.send(result);
}
})
})
}
I am stuck here from last two days.
You missed one argument although it can be optional.
From Couchbase Node.js SDK document, it have 4 arguments, but you have only 3.
db.replace(req.params.id,{firstName:"Mahesh"},function(err,result){
=>
db.replace(req.params.id,{firstName:"Mahesh"}, {}, function(err,result){
With 3rd argument of empty map may work properly, but notice that Couchbase uses optimistic locking, so you require "CAS" value for original document when you modify the original to get data integrity.
the line in db.js var ottoman = require('ottoman');it's a constructor itself. Then you have two instances, and the error comes in user.js when you try to define a model, because node-ottoman needs a reference to the bucket.
You should assign the bucket in the user.js or reuse the ottoman object that you left in the db.js
model.js
// Instantiate Couchbase and Ottoman
var couchbase = require('couchbase');
var ottoman = require('ottoman');
// Build my cluster object and open a new cluster
var myCluster = new couchbase.Cluster('localhost:8091');
var myBucket = myCluster.openBucket('default');
ottoman.bucket = myBucket;
var userMdl = ottoman.model('User', {
firstName: {type:'string'},
lastName: {type:'string'},
created: {type: 'Date', default:function(){return new Date()}},
email:'string',
phone: 'string'
},{
index: {
findByID: {
by: '_id'
},
}
}) ;
// this line needs to be after you define the model
ottoman.ensureIndices(function(){});
module.exports = userMdl;
model.exports = mybucket;
You can update Couchbase document using 2 ways 1st by upsert method and second by N1qlQuery
bucket.upsert('user', {'name': 'Jay'}, {'expiry': 1}, function(err){
bucket.get('user', function(err, result) {
console.log('Have item: %j', result.value);
})
});
let query = N1qlQuery.fromString("UPDATE `"+BUCKETNAME+"` SET name='"+data.name+"' where _id ='"+id+"'");
bucket.query(query,(error,result)=>{
if(error){
console.log(error);
}
console.log(result);
});
You can delete Couchbase document using 2 ways 1st is removed method and second by N1qlQuery
bucket.remove(id, function(error, result) {
console.log("Deleted");
});
let query = N1qlQuery.fromString("DELETE FROM `"+BUCKETNAME+"` WHERE _id = '"+id+"'");
bucket.query(query,(error,result)=>{
if(error){
console.log(error);
}
console.log(result);
})
I am very new to nodejs and am not able to figure out what exactly is going on here-
i have a basic login page, in which as the user types in the values, i go to the home page, before which i check up on the database if that username exists or not. The problem i am having is that when i click on the submit button, the code never seems to reach the database function.
This is my module in app.js -
app.post('/home',function(req,res){
//check for user existence....
var str;
console.log("in home");
user_login.perform_login(req.body.username,function(err,str){
if(!err){
console.log(str+" in login");
var cookie_val=req.body.username;
res.cookie('username',cookie_val,{signed: true});
req.session.username=cookie_val;
res.render('home.ejs',{
layout: false,
locals: {varname: cookie_val},
udata: req.session.username
});
}
else{
console.log("Couldn't find it.");
res.redirect("/login");
}
});
});
This is the perform_login from user_login.js-
var mongo_db=require("./testdb.js");
exports.perform_login=function (username,callback){
mongo_db.getInfo(username,function(err,bundle){
if(!err){
//console.log("success\n"+bundle);
callback(false,bundle);
}
else{
//console.log("couldn't find it");
callback(true,null);
}
});
}
and this is the testdb.js-
var mongoose=require('mongoose');
mongoose.connect('mongodb://localhost/test');
exports.getInfo=function(value,callback){
var db=mongoose.connection;
db.on('error',console.error.bind(console,'connection error:'));
db.once('open',function(){
console.log("Connected!");
var userSchema=mongoose.Schema({
username: String,
password: String
});
var userInstances=mongoose.model('userInstances',userSchema);
userInstances.findOne({username: value},'username password',function(err,ui){
if(!err){
callback(false,ui);
}
else{
callback(true,"error");
}
});//findone
});//db.once function
}//getinfo
In the terminal i only get -"in home", after which nothing happens at all.In the browser, it seems to be processing but remains in the login page.
when i run the perform_login with an actual hard-coded value instead of req.body.username, immediately as app.js is launched, i get the proper results. So i am able to access the database somehow outside app.post().
Is there something wrong in the callbacks??
I think that you are doing a bit too much here. The symptoms you are describing are in my experience from a not connected mongoose. You do not need to manage the connection and listen on events, if it is connected. Mongoose handles this for you. You can query a model and it will return the result as soon the connection is established.
Here is an example that should work:
app.js
//initialize mongo connection
var mongooseConnection = mongoose.connect("mongodb://localhost/test");
user.js
var mongoose = require('mongoose');
var userSchema=mongoose.Schema({
username: String,
password: String
});
userSchema.statics.performLogin = function(username, callback) {
this.findOne({username: value},'username password',function(err,user){
if(!err){
callback(false,user);
}
else{
callback(true,"error");
}
});
module.exports = mongoose.model('users',userSchema);
route:
var User = require("./user");
app.post('/home',function(req,res){
User.performLogin(req.body.username, function (err, user) {
...
});
});