Reputation: 342
For the results from the "db" and "mongo" I have to wait for 2-4 minutes.
Response from /db should be empty. But /mongo should find one item and it does.
[
{
"created_at":"2016-03-07T12:03:06.797Z",
"updated_at":"2016-03-07T12:03:06.797Z",
"name":"Chris-dude",
"username":"sevilayha",
"password":"password",
"_id":"56dd6dfaf12045d5621260fe",
"__v":0
}
]
Response from /db
Proxy Error
The proxy server received an invalid response from an upstream server. The proxy server could not handle the request GET /db.
Reason: Error reading from remote server
Apache/2.2.15 (Red Hat) Server at Port 80
In /db I want to save item only, if it not exist in my MongoDB.
//server.js
var express = require('express');
var app = express();
var mongoose= require('mongoose');
var User = require('./app/models/user');
//logs
var logs = {};
function log(key, value){
logs[key]=value;
}
//app configuration
var ipaddr = process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1';
var port = process.env.OPENSHIFT_NODEJS_PORT || 8080;
//mongodb configuration
var mongoHost = process.env.OPENSHIFT_MONGODB_DB_HOST || 'localhost';
var mongoPort = process.env.OPENSHIFT_MONGODB_DB_PORT || 27017;
var mongoUser = process.env.OPENSHIFT_MONGODB_DB_USERNAME; //mongodb username
var mongoPass = process.env.OPENSHIFT_MONGODB_DB_PASSWORD; //mongodb password
var mongoDb = process.env.OPENSHIFT_APP_NAME; //mongodb database name
//connection strings
var mongoString = 'mongodb://' + mongoUser + ':' + mongoPass + '@' + mongoHost + ':' + mongoPort + '/' + mongoDb;
mongoose.connect(mongoString, function(err) {
if (err) {
log('createConnection err',err);
}
});
var chris = new User({
name: 'Chris',
username: 'sevilayha',
password: 'password'
});
log('chris',chris);
chris.dudify(function(err, name) {
if (err) throw err;
log('Your new name is ' + name);
});
app.get('/db', function(req, res) {
User.find({ username: 'sevilayha' }, function(err, user) {
if (err) throw err;
if(user.length == 0){
chris.save(function(err) {
if (err) {
log('User saved successfully!',err);
throw err;
}
});
} else {
// object of the user
log('search result',user);
res.status(200).send(user);
res.end();
}
});
});
app.get('/logs', function(req, res) {
res.status(200).send(logs);
});
// app is running!
app.get('/', function(req, res) {
res.status(200).send('OK');
});
app.get('/mongo', function(req, res) {
User.find({ username: 'sevilayha'}, function(err, data){
if (err) {
log('User.find err',err);
res.status(500).send('NOT OK' + JSON.stringify(err));
} else {
log('user.find data',data);
res.status(200).send(JSON.stringify(data));
res.end();
}
});
});
app.listen(port, ipaddr);
//app/models/user.js
var mongoose = require('mongoose');
var Schema = mongoose.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
},
created_at: Date,
updated_at: Date
});
userSchema.methods.dudify = function() {
this.name = this.name + '-dude';
};
// on every save, add the date
userSchema.pre('save', function(next) {
var currentDate = new Date();
this.updated_at = currentDate;
if (!this.created_at) this.created_at = currentDate;
next();
});
var User = mongoose.model('User', userSchema);
module.exports = User;
What am I doing wrong?
edit:
add res.end();
Upvotes: 0
Views: 417
Reputation: 342
I have the result: I declared chris as a local variable, so calling chris.save() inside of /db
caused the error. Unfortunately, this error wasn't in the logs.
I checked it with /newTom
- it's always working.
var chris = new User({ //local variable!
name: 'Chris',
username: 'sevilayha',
password: 'password'
});
chris.dudify(function(err, name) {
if (err) throw err;
log('Your new name is ' + name);
});
tom = new User({ //global
name: 'Tom',
username: 'Jonson',
password: 'admin1'
});
tom.dudify(function(err, name) {
if (err) throw err;
log('Your new name is ' + name);
});
app.get('/db', function(req, res) {
User.find({ username: 'sevilay' }, function(err, user) {
if (err) throw err;
if(user.length == 0){
chris.save(function(err) {
if (err) {
log('User saved successfully!',err);
throw err;
}
});
} else {
// object of the user
log('search result',user);
res.status(200).send(user);
res.end();
}
});
});
app.get('/newTom', function(req, res) {
User.find({ username: 'Jonson' }, function(err, user) {
if (err) throw err;
if(user.length == 0){
tom.save(function(err) {
if (err) {
log('User saved successfully!',err);
throw err;
}
});
} else {
log('search result',user);
res.status(200).send(user);
res.end();
}
});
});
Upvotes: 0
Reputation: 2611
The issue here is your /db
route isn't letting the request know that it's finished processing, so the request from Apache is timing out. You should add a res.end()
in that route when the database operations are finished
Upvotes: 1