I am doing a project where my website showcase event information and the date like example 5sep. However i want to auto update a field in which set waiting to attended, when current date is same or already exceed the setted date. I'm using mongoDB, nodeJs, mongoose.
Schema model
var mongoose = require("mongoose");
var Schema = mongoose.Schema;
var dateTime = require("node-datetime");
var paciente_schema = new Schema({
nombre: {type: String, required: true},
estado: {type: String, required: true},
fecha: {type: Date, required: true},
edad: {type: String, required: true},
sexo: {type: String, required: true},
direccion: {type: String, required: true},
contacto: {type: String, required: true}
});
module.exports = mongoose.model("Paciente", paciente_schema);
app.js
var express = require("express");
var bodyParser = require("body-parser");
var mongoose = require("mongoose");
var Paciente = require("./models/pacientes");
var dateTime = require("node-datetime");
var app = express();
mongoose.Promise = global.Promise;
var mongoDB = mongoose.connect('mongodb://localhost/pacientes', {
useMongoClient: true
});
app.use(bodyParser.json()); //leyendo parámetros de una petición JSON
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static("client")); //servido de archivos estáticos
app.get("/app/pacientes", function(req, res){
//find busca todos los datos de la DB
Paciente.find(function(err, pacientes){
if(err){
res.send("Ocurrió error obteniendo los pacientes");
}else{
res.send(pacientes);
}
});
});
//obteniendo UN paciente
app.get("/app/pacientes/:id", function(req, res){
//método findOne el cual recibe el id del paciente a buscar en la DB
Paciente.findOne({_id: req.params.id}, function(err, paciente){
if(err){
res.send("Ocurrió error obteniendo el paciente deseado");
}else{
res.json(paciente);
}
});
});
app.post("/app/pacientes", function(req, res){
//creando paciente con los datos enviados por el user en el cuerpo de la petición
Paciente.create(req.body, function(err, pacientes){
if(err){
res.send("Error al agregar paciente");
}else{
res.json(pacientes)
}
console.log("FECHA USUARIO: " + req.body.fecha); //Mark
console.log(typeof(req.body.fecha));
var fecha2 = fecha2.toDate()
var fecha = new Date();
console.log("FECHA NEW DATE " + fecha);
console.log(typeof(fecha));
if(req.body.fecha === fecha){
console.log("WWWW");
}
});
});
//actualizando paciente
app.put("/app/pacientes/:id", function(req, res){
//creamos una variable(actualiza) la cual tomará los atributos a actualizar y se enviará como un query en el método update
var actualiza = {
nombre: req.body.nombre,
estado: req.body.estado,
fecha: req.body.fecha,
edad: req.body.edad,
sexo: req.body.sexo,
contacto: req.body.contacto
};
//encontramos un paciente y lo actualizamos, pasamos el query con los atributos actualizados
Paciente.findOneAndUpdate({_id: req.params.id}, actualiza, function(err, paciente){
if(err){
res.send("Ocurrió error actualizando" + err);
}else{
res.json(paciente);
}
});
});
//borrar paciente
app.delete("/app/pacientes/:id", function(req, res){
//método para encontrar y eliminar un dato en la DB, el _id es el identificador en la DB
Paciente.findOneAndRemove({_id: req.params.id}, function(err, pacientes){
if(err){
res.send("Error al eliminar el paciente");
}else{
res.json(pacientes)
}
});
});
//app corriendo en puerto 8888
app.listen(8888, function() {
console.log("App corriendo en 8888");
});
routes.js
//Modulo que engloba los controladores, rutas y demás configuracione
var myApp = angular.module("myApp", ["ngRoute"]);
//Agregamos el módulo ngRoute como una dependencia, hace que la app sea SPA
myApp.config(function($routeProvider){
//con el módulo ngRoute agregado podemos usar el routeProvider, el cual se usa para configurar diferentes rutas
//inicio
$routeProvider
.when("/", {
templateUrl: "templates/lista.html",
controller: "empController"
})
//lista de pacientes
.when("/pacientes", {
templateUrl: "templates/lista.html",
controller: "empController"
})
//mostrar un paciente en específico
.when("/pacientes/:id/mostrar", {
templateUrl: "templates/mostrar.html",
controller: "empController"
})
//crear un paciente
.when("/pacientes/crear", {
templateUrl: "templates/crear.html",
controller: "empController"
})
//editar un paciente
.when("/pacientes/:id/editar", {
templateUrl: "templates/editar.html",
controller: "empController"
});
});
I don't know if I need to post anymore code in here, let me know about it, I'm pretty new in it. Sorry I just wanna know how can I change the status (estado) which takes 2 values either Waiting or Attended through a radio form, I wanna know how could it be automatically updated to Attended once the current date (fecha) matches the date (fecha) put in by the user.