【发布时间】:2019-06-15 16:49:18
【问题描述】:
我已经为 FreecodeCamp 构建了以下项目。
我的项目必须保存用户姓名和锻炼以及比赛时间(包括日期)。
我的问题是访问日期的集体数据数组,并且只返回日期和练习匹配 url 中的查询。
这是我返回的 JSON 代码。我只需要设置限制日期并练习匹配用户在 url 中的查询设置的查询,以返回匹配 2018-11-22T00:00:00.000Z 的日期数量(例如 localhost:3000/addme16?id=5_OYzu1QG&start=22 November 2018&end=22 November 2018&limit=2)。因此,如果查询限制设置为两个日期并返回要返回的练习,则仅返回与 2018-11-22T00:00:00.000Z 匹配的两个日期对象。
{
"_id": "5_OYzu1QG",
"username": "Timmy",
"data": [{
"description": "walking",
"duration": 1,
"date": "2018-11-22T00:00:00.000Z"
},
{
"description": "Reading",
"duration": 2,
"date": "2019-01-01T00:00:00.000Z"
},
{
"description": "Mountain Biking",
"duration": 3,
"date": "2019-01-22T00:00:00.000Z"
}
],
"__v": 0
}
我忘记添加我的架构和将查询字符串日期更改为 iso 对象日期的方法。这是我的额外代码信息,显示了我如何向我的用户数据添加练习。我只想返回与数据库中iso数据查询日期匹配的日期
//my schema
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const mySchema = new Schema ({
_id:String,
username: String,
data: [{
_id:false,
description : String,
duration: Number,
date :{type:Date}
}]
});
const ExampleClass = mongoose.model('array',mySchema);
module.exports = ExampleClass;
//Save and create new user with id in array collection in mongodb
app.post('/api/exercise/new-user',(req, res)=>{
let myTest = req.body.user.myName;
let testing = /^[a-zA-Z\s]*$/
if(testing.test(myTest) === true && myTest !== ""){
array.find({username:req.body.user.myName})
.then(user => {
if (user.length !== 0) {
return res.send({error:"Name already exists in database"});
}
if(user.length === 0){
myId = shortid.generate();
let data = new array({
username:req.body.user.myName,
_id:myId,
})
data.save(user,err=>{
if(err) throw err;
return res.send({
username:req.body.user.myName,
_id:myId
});
});
}
}).catch(err => {
if(err) throw err;
});
}else{
res.send({username:"Invalid name! Please enter a string of letters for name."})
}
});
//adding exercise to user data
app.post('/api/exercise/add',(req,res)=>{
let myTest = req.body.user.userTask;
const testing = /^[a-zA-Z\s]*$/
const myNum = /^\d+$/;
const space = /^\s+$/;
const checker = /^[0-9-]*$/
let checking = req.body.user.userDuration;
array.countDocuments({_id:req.body.user.userId},(err, count)=>{
if(count>0){
if(testing.test(myTest) === true && myTest !== ""){
if(myNum.test(checking)=== true){
let inPutDate;
let timestamp;
let d;
if(Object.keys(req.body.user.userDate).length === 0){
let date1 = Date.now();
date1 = new Date(date1);
date1.setUTCHours(0,0,0,0);
array.findOneAndUpdate({_id:req.body.user.userId},{ $push :{data:{ description : req.body.user.userTask, duration :Number(req.body.user.userDuration),date:date1}}},{new:true}).then((data)=>{
date1 = new Date(date1).toDateString();
console.log(data)
res.send({
username:data.username,
date:date1,
description:req.body.user.userTask,
duration:Number(req.body.user.userDuration),
_id:req.body.user.userId
})
}).catch(err => {
if(err) throw err;
});
}else{
timestamp = new Date(req.body.user.userDate);
d = moment(timestamp).format("YYYY-MM-DDT00:00:00.000") + "Z";
inPutDate = new Date(d);
let checking = d;
console.log(inPutDate)
console.log(typeof(inPutDate))
array.findOneAndUpdate({_id:req.body.user.userId},{ $push :{data:{ description : req.body.user.userTask, duration :Number(req.body.user.userDuration),date:inPutDate}}},{new:true}).then((data)=>{
inPutDate = new Date(inPutDate).toDateString();
console.log(data)
res.send({
username:data.username,
date:inPutDate,
description:req.body.user.userTask,
duration:Number(req.body.user.userDuration),
_id:req.body.user.userId
})
}).catch(err => {
if(err) throw err;
});
}
}else{
res.send({duration:"Please type a number value"})
}
}else{
res.send({description:"Invalid description! Please enter a string of letters for description."})
}
}else{
res.send({_id:"Please type correct id from datbase"})
}
});
})
<!-- begin snippet: js hide: false console: true babel: false -->
【问题讨论】: