简体   繁体   中英

refers to a value, but is being used as a type here

I am new in typescript and node.js and also I am Trying to use typescript and node.js I create a model with mongoose and import it to my users.service.ts file but when I use to get this error CreateUserDto' refers to a value but is being used as a type here. Did you mean 'typeof CreateUserDto'? CreateUserDto' refers to a value but is being used as a type here. Did you mean 'typeof CreateUserDto'? I create a file by the name users.ts this is my model and also create a file by the name of users.service.ts the code of these files you can find below please help me I can fix the error thank you so much.

Error Image Model: users.ts

import * as mongoose from 'mongoose';
// import IUser from "../interface/users"
import { Schema, Document } from 'mongoose';

export interface IUser extends Document {
  firstName: String,
  lastName: String,
  email: String,
  password: String
}

const UserSchema: Schema = new Schema({
  firstName: {
    type: String,
    required: true
  },
  lastName: {
    type: String,
    required: true
  },
  email: {
    type: String,
    required: true
  },
  password: {
    type: String,
    required: true
  }
}, {
  timestamps: true
});
export default mongoose.model<IUser>('User', UserSchema);

users.service.ts

import UsersDao from '../daos/users.dao';
import { CRUD } from '../../common/interfaces/crud.interface';
import CreateUserDto from "../model/users";
import { PutUserDto } from '../dto/put.user.dto';
import { PatchUserDto } from '../dto/patch.user.dto';

class UsersService implements CRUD {

    async create(resource: CreateUserDto) {
        return UsersDao.addUser(resource);
    }

    async deleteById(id: string) {
        return UsersDao.removeUserById(id);
    }

    async list(limit: number, page: number) {
        return UsersDao.getUsers();
    }

    async patchById(id: string, resource: PatchUserDto) {
        return UsersDao.patchUserById(id, resource);
    }

    async readById(id: string) {
        return UsersDao.getUserById(id);
    }

    async putById(id: string, resource: PutUserDto) {
        return UsersDao.putUserById(id, resource);
    }

    async getUserByEmail(email: string) {
        return UsersDao.getUserByEmail(email);
    }
}

export default new UsersService();

CreateUserDto is a mongoose model, this is not a valid type descriptor for typescript. You can read about all the valid ts types here .

What you want to do is use the IUser interface you created instead like so:

async create(resource: IUser) {
   return UsersDao.addUser(resource);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM