Si presentan este error “0 , rxjs_1.lastValueFrom) is not a function” Podría solucionarse con una de estas opciones.
npm i rxjs@^7
yarn add rxjs@^7
pnpm i rxjs @^7
Introducción
¿Ya tomaste el Curso de NestJS: Programación Modular?
Instalación de Docker para este curso
Instalación de Platzi Store y presentación
Database
Configuración de Docker para MongoDB
Exploración de la base de datos con Mongo Compass
Instalando y conectando MongoDB Driver
Conexión como inyectable
Ejecutando un query
Usando variables de ambiente en Mongo
Mongoose
¿Qué es Mongoose? Instalación y configuración
Implementando Mongoose en Módulos
Conectando Mongo a los servicios
Create, update y delete
Pipe para mongoid
Agregando paginación
Agregando un filtro de rango para precios
Agregando indexadores
Relaciones en MongoDB
Relaciones uno a uno embebidas
Relaciones uno a uno referenciadas
Relaciones uno a muchos embebidas
Cómo tipar relaciones uno a muchos embebidas
Relaciones uno a muchos referenciadas
Manipulación de arrays en MongoDB
Próximos pasos
Continúa con el Curso de NestJS: Autenticación con Passport y JWT
You don't have access to this class
Keep learning! Join and start boosting your career
In addition to making the connection to the database, Mongoose allows the mapping of information to standardize its structure in each MongoDB collection.
Creating entities to shape your data is a simple task thanks to Mongoose.
Assuming you need a collection in Mongoose to store products, start by creating a file called product.entity.ts
in the products module of your application.
// modules/products/product.entity.tsimport { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';import { Document } from 'mongoose'; @Schema()export class Product extends Document { @Prop({ required: true }) name: string; @Prop() description: string; @Prop({ type: Number }) price: number; @Prop({ type: Number }) stock: number; @Prop() image: string; }export const ProductSchema = SchemaFactory.createForClass(Product);
Observe the @Prop()
decorator to map each attribute of the Product class that extends Document and tell Mongoose that it is a document property. Exporting ProductSchema that, thanks to SchemaFactory that is responsible for creating and performing the data mapping, you will be able to perform the subsequent queries from the services.
Now you just have to import the entity into the module it belongs to as follows:
// modules/products/products.module.tsimport { MongooseModule } from '@nestjs/mongoose';import { Product, ProductSchema } from './entities/product.entity'; @Module({ imports: [ MongooseModule.forFeature([ { name: Product.name, schema: ProductSchema, }, }, ]) ] })export class ProductsModule {}
Import MongooseModule and you have to assign a name to the collection and inject the schema it will use.
This way, you will be creating the products
collection in your MongoDB database and you already have mapped in your application the structure of each document it will contain to avoid errors.
Contribution created by: Kevin Fiorentino.
// src/products/entities/product.entity.tsimport { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';import { Document } from 'mongoose'; @Schema()export class Product extends Document { @Prop({ required: true }) name: string; @Prop() description: string; @Prop({ type: Number }) price: number; @Prop({ type: Number }) stock: number; @Prop() image: string; }export const ProductSchema = SchemaFactory.createForClass(Product);
// src/products/products.module.ts...import { MongooseModule } from '@nestjs/mongoose';import { Product, ProductSchema } from './entities/product.entity'; @Module({ imports: [ MongooseModule.forFeature([ { name: Product.name, schema: ProductSchema, }, }, ]), ], ... })export class ProductsModule {}
Contributions 3
Questions 4
Si presentan este error “0 , rxjs_1.lastValueFrom) is not a function” Podría solucionarse con una de estas opciones.
npm i rxjs@^7
yarn add rxjs@^7
pnpm i rxjs @^7
👍
👏
Want to see more contributions, questions and answers from the community?