Mongodb 返回错误:“数据”参数必须是字符串类型或 Buffer、TypedArray 或 DataView 的实例。收到一个 MongoError 实例

时间:2021-07-20 14:28:04

标签: node.js typescript vue.js express

这是我的长架构:

import mongoose from 'mongoose'
import { conn } from '../config/connection'
import { ManagerModelInterface } from '../../types'
const managerSchema = new mongoose.Schema({
  email: {
    type: String,
    trim: true,
    lowercase: true,
    unique: true,
    index: true,
  },
  password: {
    type: String,
    required: true,
  },
  created: {
    type: Date,
    required: true,
    default: Date.now,
  },
  analytics: {
    visits: {
      amounts: {
        type: Array,
        default: []
      },
      dates: {
        type: Array,
        default: []
      }
    }
  },
  disabled: {
    type: Boolean,
    default: false,
  },
  verified: {
    type: Boolean,
    default: false,
  },
  name: {
    type: String,
    required: true,
  },
  contracts: [{
    to: {
      type: String,
    },
    hash: {
      type: String
    },
    _id: {
      type: mongoose.Types.ObjectId,
      unique: true,
      index: true,
      default: mongoose.Types.ObjectId
    },
    sended: {
      type: Date,
      required: true,
      default: Date.now,
    },
    contract: {
      type: String,
      required: true
    },
    signedContract: {
      type: String,
      default: null
    }
  }],
  notifications: [{
    _id: {
      type: mongoose.Types.ObjectId,
      default: mongoose.Types.ObjectId,
      unique: true,
    },
    received: {
      type: Date,
      required: false,
      default: Date.now
    },
    from: {
      type: String
    },
    message: {
      type: String
    },
    readed: {
      type: Boolean,
      default: false
    }
  }],
  contacts: [
    {
      name: {
        type: String,
      },
      position: {
        type: String,
      },
      number: {
        type: String,
      },
      email: {
        type: String,
      },
    },
  ],
  working_hours: {
    type: String,
  },
  adress: {
    type: String,
  },
  categories: {
    type: Array,
    default: [],
  },

  offices: [
    {

      _id: {
        type: mongoose.Types.ObjectId,
        default: mongoose.Types.ObjectId,
        unique: true,
      },
      ips: {
        type: Array,
        required: true,
        default: [],
      },
      owner: {
        type: mongoose.Types.ObjectId,
      },
      name: {
        type: String,
      },
      visible: {
        type: Boolean,
        default: true,
      },
      verified: {
        type: Boolean,
        default: false,
      },
      description: {
        images: {
          type: Array,
          default: [],
        },
        images_webp: {
          type: Array,
          default: [],
        },
        ability_to_extend: {
          type: Boolean,
          default: false,
        },
        floor: {
          type: String,
          default: '',
        },
        price: {
          type: Number,
          required: true,
        },
        max_number_employees: {
          type: Number,
          required: true,
        },
        parking_space_number: {
          type: String,
        },
        rooms: {
          type: String,
        },
        adress: {
          type: String,
          required: true,
        },
        unique_adress: {
          type: Boolean,
          default: false,
        },
        visible_from_street: {
          type: Boolean,
          default: false,
        },
        good_location: {
          type: Boolean,
          default: false,
        },

        ramp: {
          type: Boolean,
          default: false,
        },
        nearby: {
          type: String,
          default: '',
        },
        garage: {
          type: Boolean,
          default: false,
        },
        larder: {
          type: Boolean,
          default: false,
        },
        kv_m: {
          type: Number,
        },
        build_renovation_time: {
          type: Number,
        },
        heating_type: {
          type: String,
          default: '',
        },
        heading_costs: {
          type: String,
          default: '',
        },
        administrative_expenses: {
          type: String,
        },
        ventilation_and_condi: {
          type: String,
        },
        seperate_entrance: {
          type: Boolean,
          default: false,
        },
        konference_room: {
          type: Boolean,
          default: false,
        },
        kitchen: {
          type: Boolean,
          default: false,
        },
        alarm_system: {
          type: Boolean,
          default: false,
        },
        parking: {
          type: Boolean,
          default: false,
        },
        many_passenger_by: {
          type: Boolean,
          default: false,
        },
        internet: {
          type: Boolean,
          default: false,
        },
        nice_view: {
          type: Boolean,
          default: false,
        },
        energy_seritificate: {
          type: String,
        },
        modern_interior: {
          type: Boolean,
          default: false,
        },
        much_light: {
          type: Boolean,
          default: false,
        },
        terrace: {
          type: Boolean,
          default: false,
        },
        meeting_room: {
          type: Boolean,
          default: false,
        },
        steel_door: {
          type: Boolean,
          default: false,
        },
        video_cameras: {
          type: Boolean,
          default: false,
        },
        main_entrance_code: {
          type: Boolean,
          default: false,
        },
        security_watchman: {
          type: Boolean,
          default: false,
        },
        domofon: {
          type: Boolean,
          default: false,
        },
        passing_cards: {
          type: Boolean,
          default: false,
        },
        high_celling: {
          type: String,
        },
        private_wc: {
          type: Boolean,
          default: false,
        },
        shared_wc: {
          type: Boolean,
          default: false,
        },
        shower: {
          type: Boolean,
          default: false,
        },
        no_furnitures: {
          type: Boolean,
          default: false,
        },
        ability_change_interior: {
          type: Boolean,
          default: false,
        },
        nice_to_have_neighborhood: {
          type: Boolean,
          default: false,
        },
        resting_zone: {
          type: Boolean,
          default: false,
        },
        pation: {
          type: Boolean,
          default: false,
        },
      },
    },
  ],
})

export default conn.model<ManagerModelInterface>('Manager', managerSchema)

这是触发错误的方法:

public saveManager(
    manager: NewManagerInterface
  ): Promise<ManagerModelInterface> {
    let newUser = new Manager(manager)
    console.log('saving account', newUser)
    return newUser.save()
  }

我已经登录到控制台管理器对象:

saving account {                                                                                                                                                   16:39:55
  analytics: {
    visits: {
      amounts: [],
      dates: []
    }
  },
  disabled: false,
  verified: false,
  categories: [],
  _id: 60f6d22b1eb00a3ecd2afc98,
  email: 'mvvvvp@gmail.com',
  password: '$2b$10$jMzmdG.IzIjhhSEFKcTng.eTJk3Zz2uf4/iGW2DGZYwAw5AW/AkvG',
  name: 'Irmantas',
  offices: [],
  created: 2021-07-20T13:39:55.584Z,
  contracts: [],
  notifications: [],
  contacts: []
}

我试图弄清楚为什么我收到此错误但没有成功。我错过了什么吗?我使用 MongoDB。有什么办法可以得到更具体的错误代码,为什么代码不起作用?

通过添加调用 saveManager 函数的控制器进行编辑。如您所见,代码没有通过 saveManager 方法并返回错误。

class ManagerController {
  public async createManagerHandler(
    req: Request,
    res: Response,
    next: NextFunction
  ): Promise<Response> { 
    const errors = validationResult(req)
    if (!errors.isEmpty()) {
      return res.status(400).json({ errors: errors.array() })
    }

    let { email, password, name } = req.body

    // let manager = await managerService.findManagerByEmail(email)
    // if (manager)
    //   return res
    //     .status(409)
    //     .json(errorService.resError(['Manager with this email already exist']))
    try {
      let hashedPassword = await passwordService.hashPassword(password)
      let manager = await managerService.saveManager({
        email,
        password: hashedPassword,
        name,
        offices: [],
      })
      console.log('trying to create account', manager);
      let token = randomtoken(10)
      let verify = new Verify({
        ID: manager._id.toString(),
        token: token,
      })
      await verify.save()
      await emailService.sendEmail(
        `<p>Dear  ` +
        name +
        `, </p>
                  <p>Thanks for sign up. Your verification id and token is given below :  </p>
                 
                  <ul>
                      <li>User ID: ` +
        manager._id +
        `</li>
                      <li>Token: ` +
        token +
        `</li>
                  </ul>
                  <p>verify Link: <a href="http://localhost:3000/api/verifymanageraccount/${manager._id}/${token}">Verify</a></p>
                  
                  <p><strong>This is an automatically generated mail. Please do not reply back.</strong></p>
                  
                  <p>Regards,</p>
                  <p>H Manager</p>
              `,
        email,
        'Reigistration'
      )

      return res.status(200).json('Registered')
    } catch (err) {
      await errorService.writeErrorLog(err, 'createUserHandler')
      return res.status(500).json(errorService.criticalErr())
    }
  }

谢谢

0 个答案:

没有答案