ehh somebody nows why my code not read the date value?
My proyect:
input: 6136ecfc8d1db32697fca13d, dsfsd, 10, 2018-12-10
output: {“username”:“Dani”,“date”:“Mon Dec 10 2018”,"_id":“6136ecfc8d1db32697fca13d”}
const express = require('express')
const app = express()
const cors = require('cors')
require('dotenv').config()
const bodyParser = require('body-parser')
const mongoose = require("mongoose")
const { Schema } = mongoose;
app.use(cors())
// create application/json parser
var jsonParser = bodyParser.json()
// create application/x-www-form-urlencoded parser
var urlencodedParser = bodyParser.urlencoded({ extended: false })
app.use(express.static('public'))
app.get('/', (req, res) => {
res.sendFile(__dirname + '/views/index.html')
});
mongoose.connect(process.env.DB_URI, {useNewUrlParser: true, useUnifiedTopology:true})
const UsersSchema = new Schema({
username: String,
count: Number,
log: [{
description: String,
duration: Number,
date: Date
}]
})
const Users = mongoose.model("users", UsersSchema)
/*
EXCERCISE:
{
username: "fcc_test"
description: "test",
duration: 60,
date: "Mon Jan 01 1990",
_id: "5fb5853f734231456ccb3b05"
}
LOG:
{
username: "fcc_test",
count: 1,
_id: "5fb5853f734231456ccb3b05",
log: [{
description: "test",
duration: 60,
date: "Mon Jan 01 1990",
}]
}
*/
app.post("/api/users", urlencodedParser, (req, res) => {
const { username } = req.body
if (username.length == 0) {
res.json({ username: "Invalid Username" });
} else {
// only put username Why the var call same
const user = new Users({
username,
count: 0
})
// save the new user below
user.save((err, data) => {
res.json({
username: data.username,
_id: data.id
})
})
}
// its fine!
})
app.post("/api/users/:_id/exercises", urlencodedParser, (req, res) => {
const { _id } = req.params
const { description, duration, date} = req.body
// above its work!
Users.findById(_id, (err, person) => {
// above works!
// value of the data change with a object change for keys
person.count++;
let time = new Date(req.body.date.toString());
console.log(time)
person.log.push({
description,
duration,
"date": time
})
person.save();
time = time.toDateString();
res.json({
username:person.username,
description: person.description,
duration: person.duration,
"date": time,
_id: person.id
})
})
})
// app.get("/api/users/:_id/logs?")
const listener = app.listen(process.env.PORT || 3000, () => {
console.log('Your app is listening on port ' + listener.address().port)
})