#I have a compilation error when using run start:dev

13 messages Ā· Page 1 of 1 (latest)

dire creek
#

Can yuou show the EmailTemplateService's constructor and the EmailTemplateModule?

inland olive
#

Hi, this is email-template.module

import { Module } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
import { EmailTemplatesController } from './email-templates.controller';
import { EmailTemplatesService } from './email-templates.service';
import EmailTemplateSchema from './models/email-template.model';

@Module({
  imports: [
    MongooseModule.forFeature([
      { name: 'EmailTemplate', schema: EmailTemplateSchema },
    ]),
  ],
  controllers: [EmailTemplatesController],
  providers: [EmailTemplatesService],
})
export class EmailTemplatesModule {}

this the constructor in email-templates.service

import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { CreateEmailTemplateDto } from './dto/create-email-template.dto';
import { UpdateEmailTemplateDto } from './dto/update-email-template.dto';
import EmailTemplate from './models/email-template.model';
import { EmailTemplateDocument } from './models/email-template-document';

@Injectable()
export class EmailTemplatesService {
  constructor(
    @InjectModel(EmailTemplate.name)
    private emailTemplateModel: Model<EmailTemplateDocument>,
  ) {}

  async create(createEmailTemplateDto: CreateEmailTemplateDto) {
    const createdEmailTemplate = new this.emailTemplateModel(
      createEmailTemplateDto,
    );
    return await createdEmailTemplate.save();
  }

  async findAll() {
    return await this.emailTemplateModel.find().exec();
  }

  async findOne(id: string) {
    return await this.emailTemplateModel.findById(id).exec();
  }

  async update(id: string, updateEmailTemplateDto: UpdateEmailTemplateDto) {
    return await this.emailTemplateModel
      .findByIdAndUpdate(id, updateEmailTemplateDto)
      .exec();
  }

  async remove(id: string) {
    return await this.emailTemplateModel.findByIdAndDelete(id).exec();
  }
}
dire creek
#

Can you show your EmailTemplate models as well?

#

I get the feeling it's not what you think it is

inland olive
#

it's my first ever NestJS project, i have a very strong feeling that wathever it is, it's not what i think it is
email-template.model

import { Schema, model } from 'mongoose';

const EmailTemplateSchema = new Schema({
  organization_id: { type: Number, required: true },
  created_at: { type: Date, default: Date.now },
  updated_at: { type: Date, default: Date.now },
  amps: { type: Object, required: true },
  chunks: { type: Object, required: true },
  design: {
    id: { type: String, required: true },
    rows: [{ type: Object }],
    values: { type: Object },
  },
  html: { type: String, required: true },
});

export default model('EmailTemplate', EmailTemplateSchema);
dire creek
#

Ah, yeah, that would do it. Nest's docs use CreatedModel.name because the models are class based. As model is a function, the model.name brings back the 'model' string as that's the function's name, leading to modelModel for the injection token. You should instead do @InjectModel('EmailTemplate') to match what you name theinjection token in the MongooseModule.forFeature()

inland olive
#

so should i import this, in { } instead?
like import {EmailTemplate} and call it like this?

export class EmailTemplatesService {
  constructor(
    @InjectModel('EmailTemplate')
    private emailTemplateModel: Model<EmailTemplateDocument>,
  ) {}
dire creek
#

so should I import this, in {} instead
Not sure what you mean here. Your code is generally fine, you just need to update what you pass to @InjectModel() like I showed

inland olive
#

oh because when i changed @InjectModel()
i got this error you i see on this picture

dire creek
#

Then don't import it. If you don't use that variable, you don't need it, right?

inland olive
#

oh i must of missunderstood what i was importing, thank you

inland olive
#

Hello, excuse me to bother again, but after dealing with that issue, this one popped when using run start:dev

[Nest] 26556  - 03/14/2023, 4:58:12 PM   ERROR [ExceptionHandler] The 2nd parameter to `mongoose.model()` should be a schema or a POJO
Error: The 2nd parameter to `mongoose.model()` should be a schema or a POJO```
am i sending it wrong?
dire creek
#

No clue. I don't use mongoose myself, just know what Nest's wrapper is doing under the hood