Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

282
Views
Compartir interfaces mecanografiadas entre el cliente y el modelo mongoose del servidor

Estoy tratando de compartir algunas interfaces base entre el código del cliente y el código del servidor. Tengo problemas al usar las interfaces para crear modelos de datos en mongoose.

El problema que tengo es cómo acceder a la propiedad document._id en el cliente. No puedo agregar _id a la interfaz de usuario sin causar errores de compilación y no puedo acceder a _id sin declararlo.

Diseño de mi proyecto:

 /src -/client --/user.service.ts -/server --/models ---/user.model.ts -/common --/common.d.ts

usuario.servicio.ts

 import { User } from 'common' deleteUser(user: User): Promise<User> { return this.http.delete( 'http://someurl/api/users' + user._id ) .toPromise() .then( resp => resp.json().data as User ) .catch( err => this.errorHandler(err) ); }

usuario.modelo.ts

 import { model, Schema, Document } from 'mongoose'; import { User } from 'common'; let UserSchema = new Schema { firstName: String, lastName: String, email: String } export interface UserDocument extends User, Document { } export var UserModel:Model<UserDocument> = model<UserDocument>('Users', UserSchema);

común.d.ts

 declare module 'common' { export interface User { firstName: string; lastName: string; email: string; } }

Gracias por la ayuda

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Puede declarar _id como opcional:

 export interface User { _id?: string; firstName: string; lastName: string; email: string; }

O puede tener otra interfaz para un usuario con identificación:

 export interface PersistedUser extends User { _id: string; }

Y lanzarlo cuando sea necesario.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!