gpt4 book ai didi

node.js - NodeJS : Mongoose return wrong results from MongoDB although Mongo shell return correct ones

转载 作者:太空宇宙 更新时间:2023-11-04 02:07:07 25 4
gpt4 key购买 nike

我正在使用 Express 构建我的第一个 NodeJS 应用程序,并使用 MongoDB 作为数据库和 Mongoose 作为建模工具。我正在关注tutorial from MDN作为我的指导。问题是当我从我的应用程序查询 MongoDB 时,它返回错误的结果,但是当我从 Mongo shell 查询它时,我得到正确的结果。一个示例场景是下面的代码,尽管其中有文档(由 Mongo shell 正确返回),但对我的集合进行的每次计数都返回零 (0):

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 });
});
};

我可能缺少什么?任何有关我可能做错的地方的提示都将受到高度赞赏。

编辑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-\.]+@([\w-]+\.)+[\w-]{2,4})?$/},
}
);
//Export model
module.exports = mongoose.model('GeneralUser', GUserSchema);

位置.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);

旅行.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);

卡车.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 要求的模型。

我的故障排除似乎仍然没有任何结果。

最佳答案

试试这个:

Truck.count({}, callback);

关于node.js - NodeJS : Mongoose return wrong results from MongoDB although Mongo shell return correct ones,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43824145/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com