简体   繁体   English

猫鼬文档无法访问属性且无法保存

[英]mongoose document can't access property and can't save

4.11.2, 4.6.1 two version all tested node js v8.1.2 , mongodb 3.2 4.11.2、4.6.1两个版本均已测试节点js v8.1.2 ,mongodb 3.2

(async ()=>{

    //配置数据库
    console.log(`configure mongodb ...`);
    mongoManager.init(app);
    mongoManager.connectDB();

    console.log(`load models...`);
    await mongoManager.loadModels(app.conf.dir.db_schemas);

    let dbConnectConfig = app.conf.db.mongodb;
    let uri = `mongodb://${dbConnectConfig.user}:${dbConnectConfig.password}@${dbConnectConfig.server}:${dbConnectConfig.port}/${dbConnectConfig.database}`;
    console.log('uri:', uri);
    mongoose.connect(uri);
    mongoose.Promise = global.Promise;

    let userModel = mongoose.model('pub_user', require('./db-schemas/pub/user'), 'pub_user');
    let user = await userModel.findOne();
    console.log('user:',  user.name, user);
})();

console print 控制台打印

user: undefined { _id: 57c38b1573a1951a327b3485, password_hash: 'fc76c4a86c56becc717a88f651264622', type: 'A0001', phone: '13623366688', name: 'root', code: 'root@local', stop_flag: false, system_flag: true, roles: [ '4096' ], status: 1, operated_on: 2016-08-30T02:29:48.246Z, check_in_time: 2016-08-29T01:08:37.327Z, __v: 0 } 用户:未定义{_id:57c38b1573a1951a327b3485,password_hash:'fc76c4a86c56becc717a88f651264622',类型:'A0001',电话:'13623366688',名称:'root',代码:'root @ local',stop_flag:false,system_flag:true,角色: ['4096'],状态:1,已操作时间:2016-08-30T02:29:48.246Z,检查时间:2016-08-29T01:08:37.327Z,__ v:0}

the problem is the user.name is undefined; 问题是用户名未定义;

when I use callback 当我使用回调

 userModel.findOne().exec((err, user) => {
        "use strict";
       console.log('user:',  user.name, user);
 })

still use.name is undefined 仍然使用。名称未定义

the schema 模式

import mongoose from 'mongoose';
import DICT_PUB from '../../pre-defined/dictionary-pub.json';

const PUB06 = DICT_PUB["PUB06"];

const userSchema = new mongoose.Schema({
    check_in_time: {type: Date, default: Date.now},
    operated_on: {type: Date, default: Date.now},
    status: {type: Number, min: 0, max: 1, default: 1},
    code: {type: String, required: true, maxlength: 30, index: {unique: true}},
    name: {type: String, required: true, maxlength: 30},
    phone: {type: String, maxlength: 20, unique: true, index: true},
    type: {type: String, enum: Object.keys(PUB06).slice(1)},
    roles: [String],
    system_flag: {type: Boolean, default: false},
    stop_flag: {type: Boolean, default: false},
    password_hash: String,
    tenantId: {type: mongoose.Schema.Types.ObjectId, required: true,ref:'pub_tenant'}
}, { strict: false });

userSchema.pre('update', function (next) {
    this.update({}, {$set: {operated_on: new Date()}});
    next();
});

userSchema.pre('save', function (next) {
    console.log('password_hash:');
    if (!this.password_hash) {
        this.password_hash = ...
    }
    next();
});

export default userSchema;

solved when es6 module export default , and need use 解决了es6模块export default时需要使用的问题

require('path').default 

or 要么

import schema from 'path';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM