gpt4 book ai didi

node.js - 是什么导致了 NodeJS 中的错误 "Cannot set uncompiled validation rules without configuring a validator"?

转载 作者:行者123 更新时间:2023-12-03 12:16:20 25 4
gpt4 key购买 nike

这是我到目前为止所拥有的。

'use strict';

const Hapi = require("@hapi/hapi");
const Joi = require("@hapi/joi")

const server = new Hapi.Server({ host: "0.0.0.0", port: 80 });

server.route({
method: "POST",
path: "/board",
options: {
validate: {
payload: {
name: Joi.object({
name: Joi.string().min(1).max(15)
})
}
}
},
handler: async (request, h) => {
// do stuff
}
});

server.start();

这会产生错误
Error: Cannot set uncompiled validation rules without configuring a validator
at new module.exports (C:\Users\Fukatsumu\Desktop\projects\Textboard\node_modules\@hapi\hoek\lib\error.js:23:19)
at Object.module.exports [as assert] (C:\Users\Fukatsumu\Desktop\projects\Textboard\node_modules\@hapi\hoek\lib\assert.js:20:11)
at Object.exports.compile (C:\Users\Fukatsumu\Desktop\projects\Textboard\node_modules\@hapi\hapi\lib\validation.js:48:10)
at module.exports.internals.Route._setupValidation (C:\Users\Fukatsumu\Desktop\projects\Textboard\node_modules\@hapi\hapi\lib\route.js:197:43)
at new module.exports.internals.Route (C:\Users\Fukatsumu\Desktop\projects\Textboard\node_modules\@hapi\hapi\lib\route.js:122:14)
at internals.Server._addRoute (C:\Users\Fukatsumu\Desktop\projects\Textboard\node_modules\@hapi\hapi\lib\server.js:498:23)
at internals.Server.route (C:\Users\Fukatsumu\Desktop\projects\Textboard\node_modules\@hapi\hapi\lib\server.js:491:22)
at Object.<anonymous> (C:\Users\Fukatsumu\Desktop\projects\Textboard\index.js:37:8)
at Module._compile (internal/modules/cjs/loader.js:956:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
at Module.load (internal/modules/cjs/loader.js:812:32)
at Function.Module._load (internal/modules/cjs/loader.js:724:14)
at Function.Module.runMain (internal/modules/cjs/loader.js:1025:10)
at internal/main/run_main_module.js:17:11

我希望这能验证请求,但它产生了一条错误消息,说明如何修复的细节很少。

最佳答案

它应该是

'use strict';

const Hapi = require("@hapi/hapi");
const Joi = require("@hapi/joi")

const server = new Hapi.Server({ host: "0.0.0.0", port: 80 });

server.route({
method: "POST",
path: "/board",
options: {
validate: {
payload: Joi.object({
name: Joi.string().min(1).max(15)
})
}
},
handler: async (request, h) => {
// do stuff
}
});

server.start();

payload: Joi.object({ name: Joi.string().min(1).max(15) })

关于node.js - 是什么导致了 NodeJS 中的错误 "Cannot set uncompiled validation rules without configuring a validator"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61021405/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com