簡體   English   中英

FindOne 無法正確比較字符串 Express 和 MongoDB

[英]FindOne does not correctly compare string Express and MongoDB

我正在使用 Express、express-validator 和 MongoDB 對數據庫進行角色驗證,以創建一個新用戶,檢查數據庫中是否存在用戶角色。

在我使用 express-validator 的路線中,我使用 findOne 驗證角色是否存在以便能夠發布新用戶,但即使角色正確,它也總是告訴我該角色不存在。

路線:

import Role from '../models/role';
import { check } from 'express-validator';


  router.post('/', 
   [
   check('role').custom( async (role = '') => {

            let roleExists = await Role.findOne( { role } );

            if ( !roleExists ) {
                throw new Error('El Rol no es válido');
            }
            
          }  ,
        ), 


    ], newUser);

角色 Model


import { Schema, model } from "mongoose";

const RoleSchema = Schema (
  {
    role: {
        type: String,
        required: [true, 'El Rol es obligatorio']
    }
  }  
);

module.exports = model('Role', RoleSchema);

MongoDB 中的附件,請求與 postman 和 postman 響應。

MongoDB文檔

郵遞員回應

調試器 roleExists 存在,沒有 roleExist 拋出錯誤

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM