在托管上运行get请求时,我有一个CastError
,但是在本地主机上却没有这样的问题
我尝试将_id
的类型更改为String
,但没有结果。
这是我的架构和GET请求:
const JobSchema = mongoose.Schema({
_id: String,
jobTitle: {
type: String,
required: true
},
category: {
type: String,
required: true
},
jobType: {
type: String,
required: true
},
tags : {
type: String,
required: false
},
headOffice: {
type: String,
required: true
},
region: {
type: String,
required: true
},
applyLink: {
type: String,
required: true
},
jobDescription : {
type: String,
required: true
},
name: {
type: String,
required: true
},
companyStatement : {
type: String,
required: true
},
logo: {
type: String,
required: false
},
websiteURL : {
type: String,
required: true
},
email : {
type: String,
required: true
},
aboutCompany : {
type: String,
required: true
},
status : {
type: String,
required: false
},
payment : {
type: Boolean,
required: true
}
});
获取请求:
router.get('/programming', async (req, res) => {
try {
let allOtherJobsAmount = 0;
const _1dayAgo = moment().utc().subtract(1, 'days').toDate();
const bestJobs = await Job.find({
category: 'Программирование',
status: 'best',
createdAt: {$gt: _1dayAgo}
}).sort({createdAt: -1}).limit(9);
const bestJobsAmount = await Job.find({
$and: [{category: 'Программирование'}, {status: 'best'}, {createdAt: {$gt: _1dayAgo}}]
}).countDocuments();
if (bestJobsAmount > 9){
allOtherJobsAmount = 1;
} else {
allOtherJobsAmount = 10 - bestJobsAmount;
}
const allJobs = await Job.find({
category: 'Программирование'
}).sort({createdAt: -1});
const allJobsAmount = await Job.find({
category: 'Программирование'
}).countDocuments();
const allOtherJobs = await Job.find({
$or: [
{$and: [{category: 'Программирование'}, {status: {$ne: 'best'}}]},
{$and: [{category: 'Программирование'}, {status: 'best'}, {createdAt: {$lte: _1dayAgo}}]}
]
}).sort({createdAt: -1}).limit(allOtherJobsAmount);
res.json({
bestJobs: bestJobs,
allOtherJobs: allOtherJobs,
lastPostTime: allJobs[0].createdAt,
allJobsAmount: allJobsAmount
});
} catch (err) {
res.json({message: err});
}
});
我的托管结果:
{
message:
{
message: "Cast to ObjectId failed for value "programming" at path "_id"
for model "Jobs"",
name: "CastError",
stringValue: ""programming"",
kind: "ObjectId",
value: "programming",
path: "_id",
}
}
我在本地主机上的结果:
{
bestJobs: [ ],
allOtherJobs: [
{
_id: "5d4731cac7f3044310986222",
jobTitle: "new",
category: "Программирование",
jobType: "Full-Time",
tags: "Гейм-дизайн, левел-дизайн",
headOffice: "Moscow",
region: "Moscow",
applyLink: "sdfsdfds",
jobDescription: "dsffdsfdsfsd",
name: "ARKPlay3333333333333333333333333",
companyStatement: "вааццауцуа",
logo: "logo\2019-08-04T19-28-10.258Z00311.png",
websiteURL: "выаоывадоывадыавод",
email: "arkhannanov@gmail.com",
aboutCompany: "wfwfewfejl",
status: "none",
payment: false,
createdAt: "2019-08-04T19:28:10.338Z",
updatedAt: "2019-08-04T19:28:10.338Z",
__v: 0,
}
],
lastPostTime: "2019-08-04T19:28:10.338Z",
allJobsAmount: 1,
}
更新:
早上问题消失了:)