我正在創建應用程式,我在其中使用 NestJs 框架并使用帶有 mongoose ORM 的 mongodb 資料庫我有嵌套的資料結構,我試圖將其保存在資料庫中,但是當我保存它時它會拋出錯誤。以下是我的錯誤:
[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)
以下是郵遞員要求:

當我將資料發布為可以在下面的螢屏截圖中看到的原始 JSON 時,它被成功添加。那么為什么它不使用第一種方式添加

下面是我的代碼:
用戶架構.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;
}
用戶控制器.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;
}
}
有人讓我知道我做錯了什么。任何幫助將不勝感激。
uj5u.com熱心網友回復:
您無法保存,因為用戶模型是在您的服務初始化時注入的介面物件。你也不能通過啟動和訪問它的屬性來建立這個模型。
相反,您可以展開 DTO 并保存它。如果需要,您還可以從代碼中操作額外的欄位。在下面的示例中,我添加了檔案創建的日期/時間
return await new this.userModel({
...userDto,
created_at: moment.utc() //Manipulate your extra fields and set here
}).save();
如果您需要再次在控制器級別上處理資料并將該 DTO 直接傳遞給服務并保存它,您也可以設定自己的 DTO 物件
例如:
//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();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/429516.html
