I need to integrate the CRM API with my service in Nest.js. Unfortunately, it requires I implement an interface to use a custom persistence layer, in my case, Mongo. Since I'll need to instantiate the resulting class, I can't inject the model as I normally would so I tried using this on the class member variable instead. However, this results in an error that the member variable is undefined.
This is my mongoose model:
export type ZohoTokenDocument = ZohoToken & Document;
@Schema({ timestamps: true })
export class ZohoToken {
@Prop()
name: string;
@Prop({ type: String, length: 255, unique: true })
user_mail: string;
@Prop({ type: String, length: 255, unique: true })
client_id: string;
@Prop({ type: String, length: 255 })
refresh_token: string;
@Prop({ type: String, length: 255 })
access_token: string;
@Prop({ type: String, length: 255 })
grant_token: string;
@Prop({ type: String, length: 20 })
expiry_time: string;
}
export const ZohoTokenSchema = SchemaFactory.createForClass(ZohoToken);
This is the custom class I'm creating as required by Zoho Typescript API:
export class ZohoStore implements TokenStore {
@InjectModel(ZohoToken.name)
private readonly tokenModel: Model<ZohoTokenDocument>;
/**
*
* @param {UserSignature} user A UserSignature class instance.
* @param {Token} token A Token (zcrmsdk/models/authenticator/oauth_token) class instance.
* @returns A Token class instance representing the user token details.
* @throws {SDKException} if any error occurs.
*/
async getToken(user, token): Promise<any> {
const result = await this.tokenModel.findOne({ grant_token: token });
return result;
}
And in my service, I'm just instantiating this class as new ZohoStore(), which works fine until the getToken() method is called later.
The resulting error is: "nullTypeError: Cannot read property 'findOne' of undefined", which to me means the tokenModel is not being instantiated. Any idea how I can get my model injected into this class without putting it in the constructor, otherwise, I can't instantiate it with a zero-arg constructor from my service?