微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我是Hapi错误验证@ hapi / joi我无法解决

如何解决我是Hapi错误验证@ hapi / joi我无法解决

我不明白为什么,即使我已经更新了所有内容,我也有解决方案。使用任何解决方案,我都有相同的错误。 我只是尝试在其他文档中传送,我已经尝试了不使用Joi.object 我的服务代码位于文档index.js中,而我的验证正试图在route.js中进行 我也在尝试用typesafe-joi来做,但是没用

Argument of type '{ [n: number]: { method: string; path: string; handler: (req: any,h: any) => any; } | { path: string; method: string; options: { validate: { payload: ObjectSchema<any>; }; }; handler: (req: any,h: any) => Promise<...>; } | { ...; } | { ...; }; ... 31 more ...; includes
Promise<...>; } | { ...; } | { ...; }; ... 31 more ...; includes(searchElement: { ...; } | ... 2 more ... | { ...' is not assignable to type 'ServerRoute[]'.
    The types of 'pop().options' are incompatible between these types.
      Type '{ validate: { payload: Joi.ObjectSchema<any>; }; }' is not assignable to type 'RouteOptions | ((server: Server) => RouteOptions)'.
        The types of 'validate.payload' are incompatible between these types.
          Type 'ObjectSchema<any>' is not assignable to type 'RouteOptionsResponseSchema'.
            Type 'ObjectSchema<any>' is not assignable to type 'SchemaMap'.
              Index signature is missing in type 'ObjectSchema<any>'.ts(2345)

routes.js

'use strict'

const Joi = require('@hapi/joi')
const site = require('./controllers/site')
const user = require('./controllers/user')

module.exports = [
  {
    method: 'GET',path: '/',handler: site.home
  },{
    method: 'GET',path: '/register',handler: site.register
  },{
    path: '/create-user',method: 'POST',options: {
      validate: {  
        payload: Joi.object({
          name: Joi.string().required().min(3),email: Joi.string().email().required(),password: Joi.string().required().min(6)}) 
      }
    },handler: user.createuser
  },path: '/login',handler: site.login
  },{
    path: '/validate-user',options: {
      validate: {
        payload: Joi.object({
          email: Joi.string().email().required(),password: Joi.string().required().min(6)
        })
      }
    },handler: user.validateUser
  },path: '/{param*}',handler: {
      directory: {
        path: '.',index: ['index.html']
      }
    }
  }
]

index.js

 'use strict'
    const Hapi = require('@hapi/hapi')
    const handlerbars = require('handlebars')
    const inert = require('@hapi/inert')
    const path = require('path')
    const routes = require('./routes')
    const vision = require('@hapi/vision')
    const { runInThisContext } = require('vm')
    
    const server = Hapi.server({
      port: process.env.PORT || 3000,host: 'localhost',routes: {
        files: {
          relativeto: path.join(__dirname,'public')
        }
      }
    })
    
    async function init () {
      try {
        await server.register(inert)
        await server.register(vision)
    
        server.views({
          engines: {
            hbs: handlerbars
          },relativeto: __dirname,path: 'views',layout: true,layoutPath: 'views'
        })
    
        server.route(routes);
    
        await server.start()
      } catch (error) {
        console.error(error)
        process.exit(1)
      }
    
      console.log(`Servidor lanzado en: ${server.info.uri}`)
    }
    
    init()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。