在使用mongoose设计hapi js API时,面临设计嵌套模式的joi验证的问题。我是新手,请帮助解决错误
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var vahSchema = new Schema({
vehRegNumber: { type: String, required: false, unique: true},
techSpecMeta: {
make: String,
model: String,
height: Number,
vehBodyType: String,
seatingCapacity: Number,
doorCountr: Number,
stdCityMilege: Number,
stdHighwayMilege: Number,
},
servSpecMeta: {
assignedDriver: {
name: String,
contact: Number,
license: {
regNo: { type: String, required: false, unique: true},
dateOfIssue: Date,
validDate: Date,
}
},
buy: {
purchaseDate: Date,
purchaseDealer: String,
purchasePrice: Number,
},
servicing: {
servicingDate: Date,
serviceStation: String,
serviceCost: Number,
}
},
dynmSpecMeta: {
date: Date,
busRoute: {
path: String,
distance: Number,
avgTime: Number,
},
distCovered: Number,
fuelConsumption: Number,
}
})
var Vahana = mongoose.model('Vahana', vahSchema);
module.exports = Vahana;
// ================ Base Setup ========================
var Hapi = require('hapi');
var server = new Hapi.Server();
var Joi = require('joi');
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/vahanaDB');
var vahModel = require('./models/vahana');
server.connection({port: 7002});
server.register({
register: require('hapi-swagger'),
options: {
apiVersion: "0.0.1"
}
}, function (err) {
if (err) {
server.log(['error'], 'hapi-swagger load error: ' + err)
} else {
server.log(['start'], 'hapi-swagger interface loaded')
}
});
// =============== Routes for our API =======================
// POST route
server.route({
method: 'POST',
path: '/api/vehicle',
config: {
tags: ['api'],
description: 'Save vehicle data to mongodb',
notes: 'Save user vehicle to mongodb',
validate: {
payload: {
vehRegNumber: Joi.string().required(),
techSpecMeta.make: Joi.string().required()
}
}
},
handler: function (request, reply) {
var vehicle = new vahModel(request.payload);
vehicle.save(function (error) {
if (error) {
reply({
statusCode: 503,
message: error
});
} else {
reply({
statusCode: 201,
message: 'User Saved Successfully'
});
}
});
}
});
// =============== Start our Server =======================
server.start(function () {
console.log('Server running at:', server.info.uri);
console.log('I am running on localhost:7002');
});
运行后我遇到以下错误 nodemon server.js
techSpecMeta.make:Joi.string()。required() ^
SyntaxError:意外的令牌
答案 0 :(得分:0)
如果你想要一个像" techSpecMeta.make"它需要在引号中。
payload: {
vehRegNumber: Joi.string().required(),
'techSpecMeta.make': Joi.string().required()
}