How do I solve this Joi problem(Using express, [email protected],[email protected], ,mongoose) I am trying to link two mongoose schemas using ref as
{
...
enroledcourses: [{
type: Schema.Types.ObjectId,
ref: "courses"
}]
}
joi validation
...
enroledcourses: Joi.array().items(Joi.ObjectId()).required()
the course collection is fine i have imported it at index.js as below
const Joi = require("joi")
Joi.objectId = require("joi-objectid")(Joi);
but i end up getting this error enter image description here
AssertionError [ERR_ASSERTION]: you must pass Joi as an argument at joiObjectId (D:\Programing projects\2021\ikodeafrika\ikodeafrikabackend\node_modules\joi-objectid\index.js:6:3) at Object. (D:\Programing projects\2021\ikodeafrika\ikodeafrikabackend\index.js:2:39)
index.js:2:39 being the joi.objectId import above plus i'm getting this when i hover over the joi.objectId import statement
Property 'objectId' may not exist on type 'Root'. Did you mean 'object'?ts(2568) index.d.ts(1998, 9): 'object' is declared here. any
CodePudding user response:
The problem is because [email protected]
is not compatible with [email protected]
.
Fix:
Upgrade joi-objectid
to v4.0.2
About the Typescript error (Property 'objectId' may not exist on type 'Root'), you need to augment the type of Joi
to add the objectId
property. This can be done by creating a declaration file:
// src/types/joi.d.ts
import { Schema } from "joi";
declare module "joi" {
interface Root {
objectId(): Schema;
}
}