【问题标题】:how insert a datetime in mongoose js如何在猫鼬js中插入日期时间
【发布时间】:2020-01-13 05:01:58
【问题描述】:

如何在 mongoose js 中插入日期时间?,我知道日期时间是 Schema 中的日期。

这是我的架构:

 const mongoose = require('mongoose');
 const { Schema } = mongoose;

 const todoSchema = new Schema({
    name: String,
    description: String,
    isDone: Boolean,
    createdAt: Date,
    updatedAt: Date
   });
  mongoose.model('todos', todoSchema);

这是我的发帖路线:

 app.post('/api/todos', async (req, res) => {

      const { name, description, isDone,createdAt,updatedAt} = req.body;

        const todo = new Todo({
                    name,
                    description,
                    isDone,
                    createdAt,
                    updatedAt
                              });

                try {
                    let newTodo = await todo.save();
                     res.status(201).send(newTodo);
                     } catch (err) {
                        if (err.name === 'MongoError') {
                              res.status(409).send(err.message);
                             }
                             res.status(500).send(err);
                             }
                     });

这是我的日期时间选择器输入:

  import React from 'react';
  import DateTimePicker from 'react-widgets/lib/DateTimePicker';
  import Moment from 'moment';
  import momentLocalizer from 'react-widgets-moment';

 Moment.locale('en');

 momentLocalizer();

 const DateTimePickerInput = ({
     label,
     input,
     format,
     width,
     placeholder,
     defaultValue,
     meta: { touched, error },
   }) => {
        return (
                <div className='form-group'>
                <label forname={input.name}>{label}</label> <br />
                  <DateTimePicker
                     placeholderText={placeholder}
                     className='form-control'
                      />
                <div className='text-danger' style={{ marginBottom: '20px' }}>
                {touched && error}
                 </div>
                  </div>
                      );
                       };
           export default DateTimePickerInput;

这是我的带有日期时间选择器字段的待办事项表单:

       <Field
            name='createdAt'
            component={DateTimePickerInput}
            dateFormat='dd-MM-yyyy'
            // dateFormat='dd-MM-yyyy H:mm'
            // showTimeSelect
            // timeFormat='HH:mm'
            placeholder='todoCreatedAt...'
            label='CreatedAt'
             />
          <Field
               name='updatedAt'
               component={DateTimePickerInput}
               dateFormat='dd-MM-yyyy'
               // dateFormat='dd-MM-yyyy H:mm'
               // showTimeSelect
               //timeFormat='HH:mm'
               placeholder='todoUpdatedAt...'
               label='UpdatedAt'
                 />

如何在数据库中插入日期时间,在这种情况下,createdAt 和 updatedAt。也许我做错了邮寄路线。 我希望 createdAt 和 updatedAt 保存在数据库中,但实际输出这些数据并未保存在数据库中。

怎么了?

【问题讨论】:

    标签: node.js reactjs mongoose


    【解决方案1】:

    对于 createdAt 和 updatedAt,在架构中使用 mongoose 的时间戳选项。它将自动处理时间戳。使用该选项后,您的架构将如下所示。

    const todoSchema = new Schema({ name: String, ...}, { timestamps: true });

    Read more here

    【讨论】:

    • 我是这样做的,但不起作用。 const mongoose = require('mongoose');常量 { 架构 } = 猫鼬; const todoSchema = new Schema({ name: String, description: String, isDone: Boolean, createdAt: {type: Date}, updatedAt: {type: Date} }, {timestamps: true} ); mongoose.model('todos', todoSchema);
    • 使用时间戳选项后,您不必在架构中显式添加 updatedAt 和 createdAt。
    • 我很困惑。我在架构之后声明的时间戳选项。
    • 您的新架构应如下所示。 const todoSchema = new Schema({ name: String, description: String, isDone: Boolean }, { timestamps: true } );此外,您需要创建一个新文档来对此进行测试。旧文档不会自动更新。
    • 如何创建新文档?,我从架构和发布路线中删除了 created_at 和 updated_at。
    猜你喜欢
    • 2018-12-26
    • 1970-01-01
    • 2014-07-14
    • 1970-01-01
    • 2020-02-10
    • 2016-04-08
    • 2017-04-29
    • 1970-01-01
    • 2016-06-10
    相关资源
    最近更新 更多