我正在编写一个程序来演示-使用JOI验证嵌套对象和数组的用户输入验证。
我收到以下错误:arraystring is not defined
。我该如何纠正该错误?
app.js
const Joi = require('joi');
const arrayString = ['banana','bacon','cheese'];
const arrayObjects = [{example: 'example1'}, {example: 'example2'}, {example: 'example3'}];
const userInput = {personalInfo: {
streetAddress : '123987987',
city : 'cuba',
state : 'havana'
},
preferences : arraystring };
const personalInfo = Joi.object().keys({
streetAddress : Joi.string().trim().required(),
city : Joi.string().trim().required(),
state : Joi.string().trim().length(2).required()
});
const preferencesSchema = Joi.array().items(Joi.object().keys({
example: Joi.string().required()
}));
const schema = Joi.object().keys({
personalInfo : personalInfoSchema,
preferences : preferencesSchema
});
Joi.validate(userInput,schema,(err,result)=> {
if(err)
console.log(err)
else
console.log(result);
});
错误:
F:\DatabaseProject9\app.js:14
preferences : arraystring };
^
ReferenceError: arraystring is not defined
at Object.<anonymous> (F:\DatabaseProject9\app.js:14:15)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
PS F:\DatabaseProject9> node app.js
F:\DatabaseProject9\app.js:14
preferences : arraystring };
^
ReferenceError: arraystring is not defined
at Object.<anonymous> (F:\DatabaseProject9\app.js:14:15)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:754:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
答案 0 :(得分:0)
您必须将preferences : arraystring
替换为preferences : arrayString
,因为您声明了arrayString
,而不是arraystring
const Joi = require('joi');
const arrayString = ['banana', 'bacon', 'cheese'];
const arrayObjects = [{example: 'example1'}, {example: 'example2'}, {example: 'example3'}];
const userInput = {
personalInfo: {
streetAddress: '123987987',
city: 'cuba',
state: 'havana'
},
preferences: arrayObjects
};
const personalInfoSchema = Joi.object().keys({
streetAddress: Joi.string().trim().required(),
city: Joi.string().trim().required(),
state: Joi.string().trim().required()
});
const preferencesSchema = Joi.array().items(Joi.object().keys({
example: Joi.string().required()
}));
const schema = Joi.object().keys({
personalInfo: personalInfoSchema,
preferences: preferencesSchema
});
Joi.validate(userInput, schema, (err, result) => {
if (err)
console.log(err);
else
console.log(result);
});