Mongoose.populate()没有显示DB的任何变化

时间:2016-03-30 07:53:39

标签: node.js mongoose

我一直试图从用户表填充并且一直没有成功。任何帮助将不胜感激。

我正在检查变量public IHttpActionResult Test() { List<ProductPreview> gridLines; var cs = ConfigurationManager.ConnectionStrings["eordConnection"].ConnectionString; using (SqlConnection conn = new SqlConnection(cs)) { DynamicParameters dynamicParameters = new DynamicParameters(); dynamicParameters.Add("UserID",1,ParameterDirection.Input); // Fill the Count in this Parameter dynamicParameters.Add("Count",0,ParameterDirection.Output); gridLines = conn.Query<ProductPreview>("dbo.myStoredProcedure", dynamicParameters, commandType: CommandType.StoredProcedure).ToList(); var totalCount = dynamicParameters.Get<int>("Count"); } .... return Ok(gridLines); }

isProvider

我希望用户表是一个Auth表,所以我想在这些模型中填充一个名为“userId”的字段。 id正在保存。当我打印populate的结果时,它会显示一个填充的json,但是当我在数据库中看到它时,它只显示了Id。我想通过摄影师表访问用户表的详细信息。我如何实现这一目标?

用户模型

if(true) 
   then the data is saved in a provider table 
else 
    in a customer table. 

提供者模型

/* 
 * Title: User model
 */
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
passportLocalMongoose = require('passport-local-mongoose');
var bcrypt = require('bcrypt-nodejs');
//Data model
var UserSchema = new Schema({
    email: {
        type: String,
        unique: true,
        required: true
    },
    password: {
        type: String,
        required: true
    },
    token: String,
    mobile: String,
    type: String,
    createdOn: {type: Date, default: Date.now},
    lastModifiedOn: {type: Date},
    deleted: {type: Number, default: 0},
    isPhotographer: {type: Boolean, default: false},
    verified: {type: Boolean, default: false}
});


UserSchema.pre('save', function(next) {
    var user = this;
    if(this.isModified('password') || this.isNew) {
        bcrypt.genSalt(10, function (err, salt) {
            if(err) {
                return next(err);
            }
            bcrypt.hash(user.password, salt, null, function (err, hash) {
                if ( err) {
                    return next(err);
                }
                user.password = hash;
                next();
            });
        });
    } else {
        return next();
    }
});

UserSchema.methods.comparePassword = function (passw, cb) {
    bcrypt.compare(passw, this.password, function( err, isMatch) {
        if(err) {
            return cb(err);
        }
        cb(null, isMatch);
    });
};

UserSchema.plugin(passportLocalMongoose);

user = mongoose.model('User', UserSchema);
module.exports = user;

客户模式

var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var User = require('./User');

//Data model
var providerSchema = new Schema({
    userId: {
        type: Schema.Types.ObjectId,
        ref: 'User'
    },
    firstName: String,
    lastName: String,
    profilePicture: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'GFS'
    },
    email: String,
    phone: Number,
    address: String,
    dob: Date,
    createdOn: {type: Date, default: Date.now},
    lastModifiedOn: {type: Date},
    deleted: {type: Number, default: 0},

});

providerSchema.pre('save', function(next) {
    this.lastModifiedOn = new Date;
    next();
});

provider= mongoose.model('provider', providerSchema);
module.exports = provider;

控制器

var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var User = require('./User');
//Data model
var customerSchema = new Schema({
    userId: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User',
    },
    firstName: String,
    lastName: String, 
    createdOn: {type: Date, default: Date.now},
    lastModifiedOn: {type: Date},
    deleted: {type: Number, default: 0},

});

customerSchema.pre('save', function(next) {
    this.lastModifiedOn = new Date;
    next();
});

customer = mongoose.model('Customer', customerSchema);
module.exports = customer;

1 个答案:

答案 0 :(得分:0)

我认为这是对的。 Populate不会更改数据库中的值,仅在代码运行时检索值。