桑子. 2023-07-09 00:34 采纳率: 60%
浏览 20
已结题

报错MongooseError: document must have an _id before saving

    register:async (req,res)=>{
        const {username,password,account,phone} = req.body
        // const token = JWT.generate({name:username},"30s")
        // var payload = JWT.verify(token)
        // console.log(payload,'222');
        await UserService.register({username:String(username),password:String(password),
            account:String(account),phone:String(phone)})
        res.send({
            ActionType:"OK"
        })
    }

const mongoose = require("mongoose")
const Schema = mongoose.Schema

const UserType = {
    _id:mongoose.Schema.Types.ObjectId,
    username:String,
    password:String,
    gender:Number,
    introduction:String,
    avatar:String,
    account:String,
    phone:String
}

const UserModel = mongoose.model("user",new Schema(UserType))
module.exports = UserModel

const UserModel = require("../../models/UserModel")


const UserService = {
    login:async({username,password})=>{
        return UserModel.find({   //中括号去掉,否则查出的对象不能转换成数组
            username,
            password
        })
    },
    register:async({username,password,account,phone})=>{
        return UserModel.create({
            username,password,account,phone
        })
    }
}

module.exports = UserService

  • 写回答

2条回答 默认 最新

查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 10月4日
  • 已采纳回答 9月26日
  • 创建了问题 7月9日