猫鼬中的 Date.now() 和 Date.now 有什么区别?

编程入门 行业动态 更新时间:2024-10-27 11:17:18
本文介绍了猫鼬中的 Date.now() 和 Date.now 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在猫鼬模型中使用 Date.now() 和 Date.now.

I am using Date.now() and Date.now in mongoose model.

我对它们之间的区别有点困惑.你能帮我吗?

I am a little bit confused about the difference between them. Could you please help me?

推荐答案

Date.now 可用于您的 Mongoose 架构定义中以设置字段的默认值,而 Date.now() 是等效的 JavaScript.例如,在架构定义中设置默认值时,您可以使用 Date.now.使用此架构定义,Mongoose 将使用当前时间填充 createdDate.

Date.now can be used in your Mongoose schema definition to set a default value for a field, whereas Date.now() is the JavaScript equivalent. For example, when setting a default value in your schema definition, you use Date.now. With this schema definition, Mongoose will populated createdDate with the current time.

var mongoose = require('mongoose'); var Schema = mongoose.Schema; //schema var yourSchema= new Schema({ text: {type: String}, createdAt: {type: Date, default: Date.now} });

但是,当针对您的架构编写 JavaScript 代码时,您必须使用 Date.now()

However, when writing JavaScript code against your schema, you have to use Date.now()

yourSchema.pre('save', function doSomething(next){ var something = this; something.createdAt(Date.now()); next(); });

更多推荐

猫鼬中的 Date.now() 和 Date.now 有什么区别?

本文发布于:2023-10-24 02:54:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1522733.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有什么区别   猫鼬中   Date

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!