首页 > 解决方案 > Typescript .then() 函数没有为变量赋值

问题描述

我有以下代码:

var roomStatementToBeCreated = new RoomStatement();
var roomId = this.bookingRequest.roomDetails.allocations[0].roomId;
this.roomService.get(roomId).then((room) => {
    roomStatementToBeCreated.buildingId = +room.building.id_original;
    roomStatementToBeCreated.roomTypeId = +room.roomType.id_original;
    roomStatementToBeCreated.roomNumber = +room.id_original;
}); 

在 .then() 中,所有变量都获得了它们的值。但是,之后它必须保存在数据库中,并且保存为 0 值。并且在调试时它说离开 .then() 后它们具有未定义的值。

我认为这个问题与范围有关,但是我不确定 roomStatementToBeCreated.buildingId 和其他人如何将它们的值保存在 .then() 之外。

标签: typescriptscope

解决方案


推荐阅读