1
我使用Express來構建我的第一個使用MongoDB作爲數據庫和Mongoose的NodeJS應用程序,因爲它是建模工具。我遵循tutorial from MDN作爲我的指導。 問題是,當我從我的應用程序查詢MongoDB它返回錯誤的結果,但是當我從Mongo shell查詢它時,我得到正確的結果。 一個示例場景是用下面的代碼,它返回零(0)對我的藏品做雖然有在那裏的文件(這是正確地蒙戈外殼返回)每計數:NodeJS:Mongoose從MongoDB返回錯誤的結果,儘管Mongo shell返回正確的結果
var async = require('async');
var GeneralUser = require('../models/guser');
var Truck = require('../models/truck');
var Trip = require('../models/trip');
var TruckLocation = require('../models/location');
exports.index = function(req, res, next) {
async.parallel({
user_count: function(callback) {
GeneralUser.count(callback);
},
truck_count: function(callback) {
Truck.count(callback);
},
trip_count: function(callback) {
Trip.count(callback);
},
location_count: function(callback) {
TruckLocation.count(callback);
},
}, function(err, results) {
res.render('index', { title: 'My App Home Page', error: err, data: results });
});
};
什麼是它我可能會失蹤?任何暗示,我可能會做錯誤的方式將受到高度讚賞。
EDIT 1
guser.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var GUserSchema = Schema(
{
firstName: {type: String, required: true, maxlength: 100},
lastName: {type: String, required: true, maxlength: 100},
username: {type: String, required: true, unique: true, minlength: 6, maxlength: 20},
password: {type: String, required: true, minlength: 8, maxlength: 20},
gender: {type: String, required: true, enum: ['Male','Female'], default: 'Male'},
bio: {type: String, match: /[aA-zZ]/, default: ''},
companyName: {type: String, maxlength: 100, default: ''},
physicalAddress: {type: String, required: true},
city: {type: String, required: true, maxlength: 100},
country: {type: String, required: true, maxlength: 100},
mobileNo: {type: String, required: true, match: /[0-9]{12}/, default: '255656516903'},
email: {type: String, required: true, unique: true, match: /^([\w-\.][email protected]([\w-]+\.)+[\w-]{2,4})?$/},
}
);
//Export model
module.exports = mongoose.model('GeneralUser', GUserSchema);
location.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var LocationSchema = Schema(
{
locationName: String,
geo: {type: [Number], index: '2dSphere'},
tripID: {type: Schema.ObjectId, ref: 'Trip', required: true},
loggedDate: {type: Date, default: Date.now},
}
);
//Export model
module.exports = mongoose.model('TruckLocation', LocationSchema);
trip.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var TripSchema = Schema(
{
startingLocation: {type: String, required: true, maxlength: 100},
destination: {type: String, required: true, maxlength: 100},
routeName: {type: String, required: true, maxlength: 100},
plannedDeparture: {type: Date, required: true},
expectedArrival: {type: Date, required: true},
containerIsFull: {type: Boolean, required: true},
truckID: {type: Schema.ObjectId, ref: 'Truck', required: true},
}
);
//Export model
module.exports = mongoose.model('Trip', TripSchema);
truck.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var TruckSchema = Schema(
{
truckName: {type: String, required: true, maxlength: 100},
truckType: {type: String, required: true, maxlength: 100},
truckModel: {type: String, required: true, maxlength: 100},
containerNo: {type: String, required: true, maxlength: 10},
truckPass: {type: String, required: true, minlength: 8, maxlength: 20},
ownerID: {type: Schema.ObjectId, ref: 'GeneralUser', required: true},
}
);
//Export model
module.exports = mongoose.model('Truck', TruckSchema);
以上是我的模特按@Haroon Khan的要求。
它似乎仍然是我的故障排除導致我無處。
感謝@Haroon汗。我已經做了,但沒有成功。 – Jeemu
你能告訴我你的貓鼬綱要嗎? –