如何在猫鼬js中插入日期时间

时间:2020-01-12 20:53:50

标签: node.js reactjs mongoose

如何在猫鼬js中插入日期时间?我知道日期时间是架构中的日期。

这是我的模式:

 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保存在数据库中,但是实际输出中这些数据没有保存在数据库中。

怎么了?

1 个答案:

答案 0 :(得分:0)

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

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

Read more here

相关问题