[英]Unable to save nested data in MongoDB using NestJs and Mongoose
我正在創建應用程序,我正在使用 NestJs 框架並使用 MongoDB 數據庫和 Mongoose ORM。我有嵌套的數據結構,我試圖將其保存在數據庫中,但是當我保存它時它拋出錯誤。
以下是我的錯誤:
[Nest] 11196 - 19/02/2022, 6:01:30 pm ERROR [ExceptionsHandler] Cannot set property 'city' of undefined
TypeError: Cannot set property 'city' of undefined
at UserService.addUser (D:\nest\nested-schema-proj\src\user\user.service.ts:18:27)
at UserController.addUser (D:\nest\nested-schema-proj\src\user\user.controller.ts:11:33)
以下是我的 Postman 請求:
當我將數據發布為原始 JSON(可以在下面的屏幕截圖中看到)時,它已成功添加。 那為什么不使用第一種方式添加呢?
下面是我的代碼:
用戶.schema.ts
import mongoose from "mongoose";
const addressSchema = new mongoose.Schema({
city:{type:String},
state:{type:String}
});
export const UserSchema = new mongoose.Schema({
name:{type:String},
age:{type:Number},
address:addressSchema
});
interface Address{
city:string;
state:string;
}
export interface AllUser{
name:string;
age:number;
address:Address;
}
用戶.dto.ts
export class UserDto{
name:string;
age:number;
address:Address
}
class Address{
city:string;
state:string;
}
用戶.controller.ts
import { Body, Controller, Post } from '@nestjs/common';
import { UserDto } from './dto/user.dto';
import { UserService } from './user.service';
@Controller()
export class UserController {
constructor(private userService:UserService){}
@Post('addUser')
addUser(@Body() userDto:UserDto){
return this.userService.addUser(userDto);
}
}
用戶服務.ts
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model} from 'mongoose';
import { UserDto } from './dto/user.dto';
import { AllUser } from './schema/user.schema';
@Injectable()
export class UserService {
constructor(@InjectModel('AllUser') private readonly userModel:Model<AllUser>){}
async addUser(userDto:UserDto): Promise<AllUser>{
console.log(userDto);
const data = new this.userModel();
data.name = userDto.name;
data.age = userDto.age;
data.address.city = userDto.address.city; //Showing error in this line
data.address.state = userDto.address.state;
const result = await data.save();
return result;
//Posting data as a raw JSON as shown in 2nd screenshot
// const data = new this.userModel(userDto);
// const result = await data.save();
// return result;
}
}
我究竟做錯了什么?
您無法保存,因為用戶 model 是在您的服務初始化時注入的接口 object。 你也不能通過啟動和訪問它的屬性來實現這個 model。
相反,您可以展開 DTO 並保存它。 如果你想從你的代碼中,你也可以操縱你額外的字段。 在下面的示例中,我添加了文檔創建的日期/時間
return await new this.userModel({
...userDto,
created_at: moment.utc() //Manipulate your extra fields and set here
}).save();
如果您需要再次在 controller 級別上處理數據並將該 DTO 直接傳遞給服務並保存它,您也可以設置自己的 DTO object
例如:
//In Controller
const data = new UserDto();
data.name = userDto.name;
data.age = userDto.age;
data.address.city = userDto.address.city;
data.address.state = userDto.address.state;
data.created_at: new Date();
return await this.userService.addUser(data);
//Service:
async addUser(userDto:UserDto): Promise<AllUser>{
console.log(userDto);
return await new this.userModel({
...userDto,
created_at: moment.utc() //Manipulate your extra fields and set here
}).save();
}
在 postman 中使用 Address.city,肯定可以
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.