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

标签 node.js mongodb express mongoose

我正在使用 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/

相关文章:

javascript - Nodemailer:密码字符串包含 '#' 时出现连接超时错误

javascript - 如何关闭 Node 中的无界和管道流请求?

node.js - React 没有在简单的 React 组件中定义(通用)

mongodb - Node.js - Mongoose/MongoDB - 模型架构

Node.js express : passing parameters between client pages

node.js - ExpressJS 多域 https 托管

node.js - 错误 403 禁止。没有权限访问该 URL

javascript - 在 Node.js 中混合 Promise 和递归

mongodb - 如何在 mongodb 中存储 byte[] 图像以用于 Doocr 进程

python - 处理 MongoDB 和 pymongo 中的时区日期