How do I pass multiple models into a view? - node.js

I am easily and successfully passing a single model into a view in one of my express routes like this:
exports.locations = function(req, res){
Location.find(function(err, results) {
res.render('locations', { title: 'Locations', locations: results });
});
};
I have another route where I need to pass 2 result sets into the view, how do I do that? I have tried doing this, but it doesn't seem to be working:
exports.locationdetail = function(req, res) {
var packages = Package.find();
Location.findById(req.params.id, function(err, result) {
res.render('location-detail', { title: 'Location Details', location: result, packages: packages });
});
};
Edit 1
The error I am getting is:
Cannot read property 'name' of undefined
My model looks like this:
var mongoose = require('mongoose')
,Schema = mongoose.Schema;
var PackageSchema = new mongoose.Schema({
name: String,
prev_package: String,
featured: Boolean,
services: Array
});
module.exports = mongoose.model('Package', PackageSchema);
And I am using this model in another view, and everything is working like a champ.

var mongoOp = require("./models/mongo");
var async = require('async');
router.get("/",function(req,res){
var locals = {};
var userId = req.params.userId;
async.parallel([
//Load user data using Mangoose Model
function(callback) {
mongoOp.User.find({},function(err,user){
if (err) return callback(err);
locals.user = user;
callback();
});
},
//Load posts data using Mangoose Model
function(callback) {
mongoOp.Post.find({},function(err,posts){
if (err) return callback(err);
locals.posts = posts;
callback();
});
}
], function(err) { //This function gets called after the two tasks have called their "task callbacks"
if (err) return next(err); //If an error occurred, we let express handle it by calling the `next` function
//Here `locals` will be an object with `user` and `posts` keys
//Example: `locals = {user: ..., posts: [...]}`
res.render('index.ejs', {quotes: locals.user,userdata: locals.posts})
});
});

So, it looks like this was another asynchronous "gotcha". Putting this into a nested callback did the trick:
exports.locationdetail = function(req, res) {
Location.findById(req.params.id, function(err, result) {
Package.find(function (err, results) {
res.render('location-detail', { title: 'Location Details', location: result, packages: results });
});
});
};

Related

Undefined: req.file() outputs undefined even when using upload.single() middleware

// Models
var mongoose = require('mongoose');
var ProfileSchema = new mongoose.Schema({
fullName: {
type: String,
required: true
}
// profileImage: {type: String, required: true}
});
module.exports = mongoose.model('Profile', ProfileSchema)
// Controllers
var Profile = require('../models/profile');
var multer = require('multer');
var upload = multer({dest: 'uploads/'});
exports.createProfile = (upload.single('profileImage'), function (req, res, next) {
var profileData = {
fullName: req.body.fullName,
// profileImage: req.file
}
console.log(req.file);
console.log('req.file: ', JSON.stringify(req.file));
console.log(profileData);
Profile.create(profileData, function (err, profile) {
if (err) {
// console.log(err);
res.end();
return;
// res.send(err);
}
Profile.create(function (err, profiles) {
if (err) {
res.end();
// res.send(err);
return;
}
res.json(profileData);
});
});
});
I'm trying to use middleware to add text and image at the same time in the MongoDB database. However, my fields aren't populated and when I try to print it out in the console it says req.file(): undefined. I've researched on the other issues and it states using 'upload.single()' will solve the problem. In my case, it didn't! The first section is my model view(Schema), the second section is my controllers' view.

Problem with mongoose schema reference in Node

I'm new to Node, Mongoose and to everything related to Backend. I ran into a problem recently. I couldn't figure it out as the same code in exercise files of a development course seems to be the same. Nevertheless problem still doesn't resolve. Thing I'm trying to do, coding along with the tutorials, is that I want to define a Mongoose model for comments in the Mongo database for every campground section that I have displayed on the page. I have models folder where I define models for campground and comments, a seeds.js file where I dynamically add data to campgrounds (in order for it to appear with comments instantly) and then add comments. Here is what the actual code looks like for these files:
app.js-->
var express = require("express");
var app = express();
var bodyParser = require("body-parser");
var mongoose = require('mongoose');
var Campground=require("./models/campground");
var seedDB = require("./seeds");
var PORT = process.env.IP || 3200;
seedDB();
mongoose.connect('mongodb://localhost/yelp_camp', { useNewUrlParser: true },
(err, res) => {
if (err) throw err;
console.log('Database online');
});
app.use(bodyParser.urlencoded({ extended: true }));
app.set("view engine", "ejs");
//schema
app.get("/", function (req, res) {
res.render("landing");
});
app.get("/campgrounds/tornike", function (req, res) {
//get all campgrounds
res.send(Campground.name);
});
app.get("/campgrounds", function (req, res) {
//get all campgrounds
Campground.find({}, function (err, camp) {
if (err) {
console.log("ERROR");
console.log(err);
} else {
res.render("index", { campgrounds: camp });
}
});
});
app.post("/campgrounds", function (req, res) {
var name = req.body.name;
var image = req.body.image;
var desc = req.body.description;
var newCampground = { name: name, image: image, description:desc };
//create new camp and save to database
Campground.create(newCampground, function (err, newlyCreated) {
if (err) {
console.log(err);
} else {
res.redirect("/campgrounds");
}
});
});
app.get("/campgrounds/new", function (req, res) {
res.render("new.ejs");
});
//shows more info about camp
app.get("/campgrounds/:id", function (req, res) {
//find camp with provided id
Campground.findById(req.params.id, function (err, foundCampground) {
if (err) {
console.log(err);
} else {
//render show template
res.render("show", { campground: foundCampground });
}
});
});
app.listen(PORT, process.env.IP, function () {
console.log("camp");
});
seeds.js ---->
var mongoose = require("mongoose");
var Campground = require("./models/campground");
var Comment = require("./models/comment");
var data = [
{
name: "something",
image: "image URL",
description: "blah blah bla1"
},
{
name: "something",
image: "image URL",
description: "blah blah bla2"
},
{
name: "something",
image: "image URL",
description: "blah blah bla3"
}
];
function seedDB() {
Campground.deleteMany({}, function(err) {
console.log("removed camps");
data.forEach(function(seed) {
Campground.create(seed, function(err, data) {
if (err) {
console.log(err);
} else {
console.log("added campground");
Comment.create(
{
text: "dubdabdubadah",
author: "Homer"
},
function(err, comment) {
if (err) {
console.log(err);
} else {
campground.comments.push(comment);
campground.save();
console.log("Created new comment");
}
}
);
}
});
});
});
}
module.exports = seedDB;
comment.js --->
var mongoose = require("mongoose");
var commentSchema= new mongoose.Schema({
text:String,
author:String
});
module.exports=mongoose.model("Comment", commentSchema);
campground.js ---->
var mongoose = require("mongoose");
var campgroundSchema = new mongoose.Schema({
name: String,
image: String,
description: String,
comments: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "Comment"
}
]
});
module.exports = mongoose.model("Campground", campgroundSchema);
Error occurs in the seeds.js file on this line campground.comments.push(comment);
it looks like this:
[nodemon] starting `node app.js`
camp
Database online
removed camps
added campground
added campground
added campground
events.js:183
throw er; // Unhandled 'error' event
^
ReferenceError: campground is not defined
I can't actually get my head around this one as when comparing the code to the exercise files code - it is the same. The only thing I can think of is the versions of mongoose express or something else may not be relevant or some methods deprecated, but honestly no idea what's wrong. I've been trying to solve it for several days. any idea people ?
The error message is telling you that you haven't defined a variable named campground, which is correct, as you haven't.
To fix this, change data to campground in your Campground.create callback:
Campground.create(seed, function(err, campground) {

Updating a record with mongoose

I'm learning MEAN stack applications and am working on a tv watchlist app. I built the api successfully without the seasons field in the model. Now I'd like to add this field so that a user can add seasons and episodes to the document to keep track of episodes they have watched. Through trial and error I found the query I'd use in the mongo shell to update a field in the episodes object but I can't create the right syntax to do this with mongoose inside my route. Can someone look in my tele.js routes at the router.put and tell me what's wrong.
Models (TVSeries.js)
var mongoose = require('mongoose')
var Schema = mongoose.Schema
var tvSchema = new Schema({
title:String,
poster:String,
rated:String,
program_time:Number,
network:String,
airs_on:[],
streams_on:[],
genre:[],
seasons:[
season_number:Number,
episodes:[
{
episode_number:Number,
title:String,
watched:Boolean
}
]
]
}, {collection: 'tvShows'});
module.exports = mongoose.model('tv', tvSchema);
Routes (tele.js)
var express = require('express');
var router = express.Router();
var TV = require('../models/TVSeries.js');
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Your Watchlist' });
});
router.get('/api/shows/:id/seasons', function(req, res){
TV.findById(req.params.id, 'seasons', function(err, data){
if(err){console.log(err)}
else{res.json(data)};
});
});
router.put('/api/shows/:id/seasons/:sid', function(req, res){
var setField = {
seasons:[
{
season_number:req.params.sid,
episodes:[
req.body
]
}
]
}
TV.findOneAndUpdate({"_id":req.params.id}, setField, {upsert:true}, function(err, results){
if(err){console.log(err)}
else{
console.log(setField);
res.json(results)
}
})
})
module.exports = router;
Mongo Shell command
db.tvShows.update({"_id":ObjectId('######################')},{$set: {'seasons.1.episodes.1.title':'This is my title change'}})
You can use &elementMatch to find the desire season in the array, and in the setField object you can use the positional $ operator which identify the element matched in the query.
The problem is that if it doesn't find any season that match the season_number, the document will not be updated. In this case you can set another update query to add this season in the seasons array.
router.put('/api/shows/:id/seasons/:sid', function(req, res){
var query = {
"_id": req.params.id,
"seasons": {
$elemMatch: {
"season_number": req.params.sid
}
}
}
var setField = {
$addToSet: {
"seasons.$.episodes": req.body
}
}
TV.findOneAndUpdate(query, setField, {upsert:true}, function(err, results){
if (err && err.code == 16836) { // no document was matched
var season = {
"season_number": req.params.sid
"episodes": [ req.body]
}
TV.findOneAndUpdate({"_id": req.params.id}, {$push: {"seasons": season}} , function(err, result) {
console.log("Inserted document in array");
res.json(result)
});
}
else if (err){
console.log(err);
res.status(500).send('Something wrong!');
}
else {
console.log(setField);
res.json(results)
}
})
})
You can see here some mongodb array operators.
Hope it helps.

Mongoose find not working with ObjectId

I have one schema defined in userref.js
module.exports = (function userref() {
var Schema = mongoose.Schema;
var newSchema= new Schema([{
userId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
index: true
},
value: { type: Number }
}]);
var results = mongoose.model('UserRef', newSchema);
return results;
})();
I have inserted some data and when I try to fetch some data I am getting proper values from mongodb console
db.getCollection('userrefs').find({'userId':ObjectId('57a48fa57429b91000e224a6')})
It returns properly some data
Now issue is that when I try to fetch some data in code by giving objectId I am getting empty array. In below function userrefs is returned as empty array
//req.params.userId=57a48fa57429b91000e224a6
var UserRef = require('../userref.js');
this.getuserref = function (req, res, next) {
try {
var o_userId =mongoose.Types.ObjectId(req.params.userId);
var query = { userId: o_userId };
var projection = '_id userId value';
UserRef.find(query, projection, function (err, usrrefs) {
if (err) return next(err);
res.send(usrrefs);
console.log("userref fetched Properly");
});
} catch (err) {
console.log('Error While Fetching ' + err);
return next(err);
}
};
Also when I debug code I can see o_userId as objectId with id value as some junk character
o_userId: ObjectID
_bsontype: "ObjectID"
id: "W¤¥t)¹â$¦"
Try this:
try {
var o_userId =mongoose.Types.ObjectId(req.params.userId);
var query = { userId: o_userId };
var projection = '_id $.userId $.value';
UserRef.find(query, projection, function (err, usrrefs) {
if (err) return next(err);
res.send(usrrefs);
console.log("userref fetched Properly");
});
} catch (err) {
console.log('Error While Fetching ' + err);
return next(err);
}
Add the export like this
module.exports.modelname= mongoose.model('userrefs', nameofschema, 'userrefs');
var z = require('../userref.js');
var UserRef = z.modelname;
Now call using UserRef.
Just simply try this man.
Model.find({ 'userId': objectidvariable}, '_id userid etc', function (err, docs) {
// docs is an array
});
Reference sample copied from their official doc.

I received an error when I delete an index from elasticsearch

I received an error when I manually deleted an index from elasticsearch. This happen after manually deleted and I use User.search function in the route. This is the error:
Error: [search_phase_execution_exception] all shards failed
The reason why I manually deleted the index is because mongoosastic has a known issue where, whenever I delete documents from mongodb, elasticsearch still has the documents with it.
Here's the code
models/user.js
var mongoose = require('mongoose');
var mongoosastic = require('mongoosastic');
var Schema = mongoose.Schema;
var UserSchema = new Schema({
private: false,
twitter: String,
tokens: Array,
username: String,
displayName: String,
picture: String,
});
UserSchema.plugin(mongoosastic, {
hosts: [
'localhost:9200'
]});
module.exports = mongoose.model('User', UserSchema);
router.js
User.createMapping(function(err, mapping) {
if (err) {
console.log('error creating mapping (you can safely ignore this)');
console.log(err);
} else {
console.log('mapping created!');
console.log(mapping);
}
});
var stream = User.synchronize();
var count = 0;
stream.on('data', function(err, doc){
count++;
});
stream.on('close', function(){
console.log('indexed ' + count + ' documents!');
});
stream.on('error', function(err){
console.log(err);
});
/* The result for searching for User's */
router.get('/search', function(req, res, next) {
console.log(req.query.q);
if (req.query.q) {
User.search({
query_string:
{ query: req.query.q }
}, function(err, results) {
if (err) return next(err);
console.log(results);
var data = results.hits.hits.map(function(hit) {
return hit;
});
console.log(data);
return res.render('main/search_results', { data: data });
});
}
});

Resources