我收到来自Mongoose FindOne的空响应,如果我把参数作为dateString参数。我使用nodejs和猫鼬。猫鼬FindOne不工作
这里是我的代码:
var Service = app.models.service;
controller.newService = function(req, res) {
var date = new Date();
var dateString = date.toString();
var countHours = 1;
var user = req.decoded;
var findProfessional = function(){
console.log(countHours);
console.log(user._doc._id);
console.log(req.body.service[0].tipo);
console.log(dateString);
Service.findOne({
tipo: req.body.service[0].tipo,
client: user._doc._id,
dateOpen: dateString
}, function(err, service) {
console.log(service);
if (err) throw err;
if (!service) {
console.log("service not found");
} else if (service) {
if (service.status == 'open' && countHours <= 24) {
setTimeout(function(){
Professional.find({
'services.name': req.body.service[0].tipo
}, function(err, professional) {
if (err) throw err;
if (professional) {
DO STUFF
} else {
DO STUFF
res.json({ success: false, message: 'No professionals found' });
};
});
countHours++;
findProfessional();
}, 10000);
} else if (service.status != 'open'){
// DO STUFF
} else if (countHours > 24){
//DO STUFF
}
}
});
};
这里是我的架构:
var schemaServices = mongoose.Schema({
tipo: {
type: String,
required: true
},
client: {
type: String,
required: true
},
dateOpen: {
type: String,
required: true
},
dateClose: {
type: String,
},
professional: {
type: String
},
status: {
type: String,
required: true
},
services: {
type: [{name: String,
preco: String,
tipo: String,
tipoCusto: String,
pedido: String}],
required: true
},
visitDay: {
type: String
},
visitHour: {
type: String
},
address: {
type: [{cep: String,
street: String,
number: String,
comp: String,
district: String,
city: String}],
required: true
}
});
schemaServices.index({client: 1, tipo: 1, dateOpen: 1}, {unique: true});
什么我的代码吗?
当对该路由发出请求时,节点保存新服务并寻找专业人员完成这项工作。它一直在寻找,直到定时器计数小时达到24或有一个专业的工作。服务器正在保存服务没有问题,但我需要检查刚刚保存的服务的状态,并且当我将dateOpen:dateString放入时,我正面临Service.findOne()返回空值的问题。
问题是
Service.findOne({
tipo: req.body.service[0].tipo,
client: user._doc._id,
dateOpen: dateString
}
总是返回般的服务一个空值不存在。但是如果我在使用mongo的提示中进行了相同的查询,我得到了正确的服务。
我做了一些测试,并观察到了一些东西:
- 该console.logs打印好了它应该打印。从参数dateString在Service.findOne()它的工作原理
所以,我认为,误差与dateString有关,但我看不出它是什么:
- 当我删除dateOpen。
希望有人能帮助我,
谢谢你提前!
您正在将日期保存为字符串。检查它们的格式是否相同。否则,它不会返回所需的结果。 我建议将日期保存为日期类型。然后,您可以直接在查询中使用Date()对象来查找结果。 – Sohel
是的,看你的文件,以及你的console.log(dateString)的结果。随意在你的问题中发布一个例子。 – dyouberg
谢谢你们,刚解决。格式是一样的。 –