Expressjs + Mongoose - This webpage is not available? - node.js

Why mongoose crashes the expressjs site?
Below is my code:
var express = require('express');
var mongoose = require('mongoose');
var app = express();
// Connect to mongodb
mongoose.connect("mongodb://localhost/testdb", function(err) {
if (err) throw err;
console.log("Successfully connected to mongodb");
// Start the application after the database connection is ready
app.listen(3000);
console.log("Listening on port 3000");
});
// With Mongoose, everything is derived from a Schema. Let's get a reference to it and define our users.
var userSchema = mongoose.Schema({
name: String,
username: { type: String, required: true, unique: true },
password: { type: String, required: true },
admin: Boolean,
location: String,
meta: {
age: Number,
website: String
},
created_at: Date,
updated_at: Date
});
// The next step is compiling our schema into a Model.
var User = mongoose.model('User', userSchema);
// Set route.
app.get("/", function(req, res) {
// We can access all of the user documents through our User model.
User.find(function (err, users) {
if (err) return console.error(err);
console.log(users);
})
});
I get this on browser:
This webpage is not available
But in my terminal I get the result:
Successfully connected to mongodb
Listening on port 3000
[ { _id: 57682f69feaf405c51fdf144,
username: 'testuser1',
email: 'testuser1#testdomain.com' },
{ _id: 57683009feaf405c51fdf145,
username: 'testuser2',
email: 'testuser2#testdomain.com' },
{ _id: 57683009feaf405c51fdf146,
username: 'testuser3',
email: 'testuser3#testdomain.com' }]
Any ideas what I have missed?

The problem is that you are not writing anything in the response object in your request handler. Therefore the browser keeps waiting for the request to finish and ends up with a timeout. In your app.get(), you can update the response like this:
// Set route.
app.get("/", function(req, res) {
// We can access all of the user documents through our User model.
User.find(function (err, users) {
if (err) {
console.error(err);
// some simple error handling, maybe form a proper error object for response.
res.status(500).json(err);
}
console.log(users);
res.status(200).json(users); // setting the object as json response
//OR
// res.end(); if you don't want to send anything to the client
})
});
or something similar.
Refer the Express documentation for more details:http://expressjs.com/en/api.html#res

Related

Express server GET request userId

I want to create an GET API route for taskuser/getallusers?userId='' .
I want to get all tasks that are assigned to a specific user. But when i test this call (http://localhost:4000/api/taskuser/getalltasks?userId=5bfe4af425ddde2b04eb19c6) but no error ?
I still get all tasks assigned to any user. can someone tell me what goes wrong ?
Model taskuser:
const mongoose = require('mongoose');
const TaskuserSchema = new mongoose.Schema({
task_name:{
type: String,
required: true,
minlength: 1,
unique: true,
},
userId: {
type: mongoose.Schema.Types.ObjectId,
required: true,
},
task_category: String,
task_xpreward: Number,
task_completed: Boolean,
task_difficulty: Number,
task_city : String,
});
Api route:
router.get('/getalltasks/:userid', cors(),async(req,res) => { // Add /:userid
var userid = req.params.userid;
Taskuser.find({ userId: userid}, function(err, tasks) {
// if there is an error retrieving, send the error.
// nothing after res.send(err) will execute
if (err)
res.send(err);
res.json(tasks); // return all tasks that are in JSON format
});
});
Edit: new api route
You should update the code to :
router.get('/getalltasks/:userid', cors(),async(req,res) => { // Add /:userid
var userid = req.params.userid;
Taskuser.find({ userId: userid}, function(err, tasks) {
// if there is an error retrieving, send the error.
// nothing after res.send(err) will execute
if (err)
res.send(err);
res.json(tasks); // return all tasks that are in JSON format
});
});
Documentation : https://mongoosejs.com/docs/api.html#model_Model.find
Change your api route file to this:
router.get("/getalltasks", cors(), async (req, res) => {
var userid = req.params.userId;
Taskuser.find({ userId: userid }, function(err, tasks) {
var userid = req.params.userId;
// if there is an error retrieving, send the error.
// nothing after res.send(err) will execute
if (err) res.send(err);
res.json(tasks); // return all tasks that are in JSON format
});
});

MongoDB: mongod shows that my app is not authorized

I have installed mongodb and my mongodb and db folders are C:/mongoDB/bin and C:/data/db respectively. I have also setup admin user as stated on
https://docs.mongodb.com/manual/tutorial/enable-authentication/
Now i want to perform basic CRUD operations requiring both read and write on a database mApp through Express and Mongoose. I am providing code for both app and schema below.
Code is well documented so that it is easy to understand.
App.js
var express = require('express');
var app = express();
//Invoking user
var User = require('./schema.js');
//Creating an employee object by giving values to all properties
var User1 = new User({
name: 'Anurag',
username: 'Anurag2',
password: 'abc',
admin: false,
location: 'somewhere',
meta: {
age: 25,
website: 'abc.com'
},
createdAt: 'Jun 11 2017',
updatedAt: 'Jun 11 2017'
}); //Remember to provide all records,otherwise document wont be saved.
//CRUD start. Creating a user document
User1.save(function(err, employ, num) {
if (err) {
console.log('error occurred');
}
console.log('saved ' + num + ' record');
console.log('Details ' + employ);
});
/* To retrieve documents from database, you can retrieve all at
once, or one at a time by find(), findById(), findOne() */
//To retrieve all documents
User.find({}, function(err, data) {
if (err) {
console.log('error occurred while retrieving all docs');
}
console.log(data);
});
User.findOne({
username: 'Anurag2'
}, function(err, data) {
if (err) {
console.log('error in finding one document');
}
console.log(data);
});
User.update({
location: 'someplace'
}, {
location: 'anything'
}, function(err) {
if (err) {
console.log('error in updating');
}
console.log('updated');
});
//update one document
User.findOneAndUpdate({
username: 'Anurag2'
}, {
admin: true
}, function(err, data) {
if (err) {
console.log('error in finding and updating');
}
console.log('updated' + data);
});
//Delete a user document
User.remove({
location: 'anywhere'
}, function(err) {
if (err) {
console.log('error occurred');
}
console.log('removed');
});
DB Schema(schema.js)
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/mApp'); //myApp is the database being connected here.
//now we open a connection
var db = mongoose.connection;
db.once('open', function() {
console.log('Connected to Database');
});
db.on('error', console.error.bind(console, 'connection error'));
//initialising a schema
var Schema = mongoose.Schema;
mongoose.Promise = require('bluebird'); //used as mpromise was showing deprecated on console.
//creating a schema
var userSchema = new Schema({
name: String,
username: {
type: String,
required: true,
unique: true
},
password: {
type: String,
Required: true
},
admin: Boolean,
location: String,
meta: {
age: Number,
website: String
},
createdAt: Date,
updatedAt: Date
});
//creating a model that uses this schema
var User = mongoose.model('User', userSchema);
//now we export this model
module.exports = User;
Now, I login in mongo through admin and i changed the db to mApp. I run the app through node.
The mongod console shows I am not authorized to perform any actions on the app.
No query gets executed and I get all error messages. Why is this happening? Please help me with this.
You have been enable authentication for your database.
So, you have to provide the corresponding credentials in your connection string
Change:
mongoose.connect('mongodb://localhost/mApp');
To
mongoose.connect('mongodb://username:password#host:port/database');
More information on mongoose documentation

Elasticsearch returns IndexMissingException

I'm trying to implement a search engine in node.js using elasticSearch + mongoose which is elmongo. Whenever i try to run a search api i get
"error": "IndexMissingException[[ads] missing]"
Here's the code
advertisingSchema.js
var mongoose = require('mongoose');
var elmongo = require('elmongo');
var Schema = mongoose.Schema;
var AdSchema = new Schema({
title: String,
description: String,
category: String,
phoneNumber: { type: Number, unique: true},
photos: [{ type: String }],
created: { type: Date, default: Date.now},
price: Number,
password: String
});
AdSchema.plugin(elmongo);
module.exports = mongoose.model('Ad', AdSchema
);
api.js
var Ad = require('../models/advertising');
module.exports = function(app, express) {
var api = express.Router();
Ad.sync(function(err) {
console.log("Check the number sync");
})
api.post('/search', function(req, res) {
Ad.search(req.body, function(err, result){
if(err) {
res.send(err);
return;
}
res.json(result);
});
});
return api;
}
I've done everything correctly, but its just doesn't want to return the search result.
As the error suggest there is no index named 'ads' in your cluster. Index is automatically created unless you have set the property "action.auto_create_index" to false in your elasticsearch configuration. You can create the index programmatically or by running a curl request.
Refer to create index api.

Mongoose - findByIdAndUpdate - doesn't work with req.body

I have a problem with update documents in mongodb over mongoose.
My model bellow:
var mongoose = require('mongoose');
var bcrypt = require('bcrypt-nodejs');
var UserSchema = new mongoose.Schema({
first_name:{
type: String
},
last_name:{
type: String
},
email:{
type: String,
unique: true,
required: true
},
password:{
type: String,
required: true
},
is_active:{
type: Boolean,
default: true
},
last_login:{
type: Date
}
});
module.exports = mongoose.model('User', UserSchema);
Controller put function bellow:
exports.updateUser = function (req, res) {
console.log(req.body);
User.findByIdAndUpdate(req.body.user_id, {$set:req.body}, function(err, result){
if(err){
console.log(err);
}
console.log("RESULT: " + result);
});
res.send('Done')
}
Output on console:
Listening on port 3000... { first_name: 'Michal', last_name: 'Test' }
PUT /api/users/54724d0fccf520000073b9e3 200 58.280 ms - 4
The printed params are provided as form-data (key-value). Looks that is not working at least for me any idea what is wrong here?
You have to use req.params.user_id instead req.body.user_id
exports.updateUser = function (req, res) {
console.log(req.body);
User.findByIdAndUpdate(req.params.user_id,{$set:req.body},{new:true}, function(err, result){
if(err){
console.log(err);
}
console.log("RESULT: " + result);
res.send('Done')
});
};
I found the mistake. Note that I'm calling
req.body.user_id
where should be
req.params.user_id
url is (PUT) http://127.0.0.1:3000/api/users/54724d0fccf520000073b9e3
Further, the req.body would have key value as Text and realized as String object, inside the code. Thus, it is useful to parse the string into JSON using JSON.parse(req.body.user) - while the user is the key and { first_name: 'Michal', last_name: 'Test' } is the value.
console.log(req.body);
var update = JSON.parse(req.body.user);
var id = req.params.user_id;
User.findByIdAndUpdate(id, update, function(err, result){
if(err){
console.log(err);
}
console.log("RESULT: " + result);
res.send('Done')
});
Note: the update value is sent to Mongo DB as
{$set: { first_name : 'Michal`, last_name: 'Test' }
Further reference: Mongoose JS documentation - findByIdAndUpdate

Express node.js with mongoose CRUD strange behavior.

I'm starting my adventure with NodeJS. I've choose express web framework with mongoose database. As part of my front-end I'm using AngularJS.
First the front end, basic form args passing to /api/user.
$scope.form = {};
$scope.submitUser = function () {
console.log($scope.form);
$http.post('/api/user', $scope.form).
success(function(data) {})
.error(function(data) {});
};
$scope.form equals to:
Object {name: "foo", surname: "bar", email: "foobar#bar.foo"}
Then we got the back-end, where I start with default user schema:
var userSchema = new mongoose.Schema({
name : String,
surname : String,
email : String
});
var User = mongoose.model('User', userSchema);
And the api post handler:
app.post('/api/user', function (req, res) {
User.create({
name : req.name,
surname : req.surname,
done : false
}, function(err, user) {
if (err)
res.send(err);
res.json(user);
});
});
And the result of that instead of object is just id with __v:
__v: 0
_id: "536e218351b1182d0f000001"
PS. This one is magic, I have completly no idea why this is happening:
Those records above __v and _id, are not being save anywhere (show collections, and find() on each of those results in null). But when I run
User.find(function(err, users) {
if (err)
res.send(err)
res.json(users);
I get few records (previous tries of passing this form) with it's __v and _id.
Connection looks like this:
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:1/test');
Thanks for any tips, explanation to that. Sorry for my lack of knowledge but I really tried.
Have a nice day! ;)
Assuming that you send the json object in the body (payload) of POST from client, The server should access the value as req.body.name, not req.name
app.post('/api/user', function (req, res) {
User.create({
name : req.body.name,
surname : req.body.surname,
done : false
}, function(err, user) {
if (err)
res.send(err);
res.json(user);
});
});
Make sure you pass body-parser middleware to express:
var express = require('express')
var app = express()
app.use(express.bodyParser()); // for express 3.x
or for express 4.x
var express = require('express')
var bodyParser = require('body-parser')
var app = express()
app.use(bodyParser()); // for express 4.x
In my case it does not work. Why?
I am getting the value from req.body.name
If I log it console.log(req.body.name); I get the value on my console.
POST:
{ name: 'typing any name', status: null }
typing any name
So the workflow between my frontend (angular.js), the form and the backend (node.js, express, mongoose) seems to work. Now I POST the value, but I get an empty object in my mongoDB.
{"_id":"543a50a974de6e2606bd8478","__v":0}
app.post('/api/offers', function (req, res){
var offer;
console.log("POST: ");
console.log(req.body);
console.log(req.body.name);
offer = new OfferModel({
name: req.body.name,
value: req.body.value,
title: req.body.title,
content: req.body.content,
});
offer.save(function (err) {
if (!err) {
return console.log("created offer" + req.body.name);
} else {
return console.log(err);
}
});
return res.send(offer);
});
And here is the model:
var offerSchema = mongoose.Schema({
offer : {
name : String,
value : String,
title : String,
content : String,
image : String,
start : String,
end : String,
targets : String,
beacons : String,
published : String
}
});
var OfferModel = mongoose.model('Offer', offerSchema);

Resources