Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
451 views
in Technique[技术] by (71.8m points)

定义了mongoose schemaType的get:方法,在获取单个信息的时候有用,获取一个数组列表就无效了

定义了mongoose schemaType的get:方法,用来格式化时间,在使用findOne() 获取单个信息的时候有用。
但是使用find(),获取一个数组列表就无效了

'use strict'
const mongoose = require('mongoose')
const moment = require('moment')
const Schema = mongoose.Schema
const UserSchema = new Schema({
    name: {
        type: String,
        required: '请填写用户名称'
    },
    password: {
        type: String
    },
    createdAt: {
        type: Date,
        default: Date.now,
        get(val){
            return moment(val).format('YYYY-MM-DD HH:mm:ss');
        }
    },
    updatedAt: {
        type: Date,
        default: Date.now,
        get(val){
            return moment(val).format('YYYY-MM-DD HH:mm:ss');
        }
    }
}, {
    timestamps: {
        createdAt: 'createdAt',
        updatedAt: 'updatedAt'
    },
    toJSON:{
        getters:true,
        virtuals:true
    },
    toObject:{
        getters:true,
        virtuals:true
    }
})

/*
UserSchema.set('toJSON', {
    getters: true,
    virtuals: true
})
UserSchema.set('toObject', {
    getters: true,
    virtuals: true
})
*/

module.exports = mongoose.model('Users', UserSchema)

以上代码这么尝试,是哪里出了问题呢


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...