.map()不是打字稿中的函数

时间:2018-07-24 02:19:15

标签: typescript typeorm

对象,JavaScript中的{}没有方法.map(),它仅以Array []的数据类型可用,因此尽管以下数据格式是正确的,但为什么我不能使用.map()

我想查询数组数据并使用.map()函数来获取其值,如下所示

我收到错误消息: .map()不是打字稿中的函数

const startDate = [];
const endDate = [];
let query;
query = this.createQueryBuilder("LeaveRequest")
      .where("LeaveRequest.employeeId = :employeeId", { employeeId })
      .getMany();
query.leaveRequest.map(function (leaveRequest) {
      leaveRequest.map(function (values) {
      startDate.push(values.startDate);
      endDate.push(values.endDate);
  });
});
return {startDate, endDate};

数据格式 当我做return query;时,得到的格式如下;

[
  {
    "id": 40,
    "createdAt": "2018-07-23T10:01:49.939Z",
    "updatedAt": "2018-07-23T10:01:49.939Z",
    "createdBy": 1,
    "updatedBy": null,
    "state": 1,
    "version": 1,
    "requestStatus": 1,
 },
{
    "id": 40,
    "createdAt": "2018-07-23T10:01:49.939Z",
    "updatedAt": "2018-07-23T10:01:49.939Z",
    "createdBy": 1,
    "updatedBy": null,
    "state": 1,
    "version": 1,
    "requestStatus": 1,
},
{
    "id": 40,
    "createdAt": "2018-07-23T10:01:49.939Z",
    "updatedAt": "2018-07-23T10:01:49.939Z",
    "createdBy": 1,
    "updatedBy": null,
    "state": 1,
    "version": 1,
    "requestStatus": 1,
}
]

您可以看到图像

enter image description here

3 个答案:

答案 0 :(得分:4)

定义query的类型并对其进行初始化,因此即使getMany返回null初始化值也不允许该.map()不是函数错误

let query: Array<any> = new Array<any>(size);

如果您已经为数组的返回结果定义了类类型,然后使用该类类型并为数组设置了大小,则不建议使用any类型。

如果您什么也不返回,请使用forEach代替map

forEach()方法实际上不返回任何内容(未定义)。它只是在数组中的每个元素上调用提供的函数。

map()方法将在数组中的每个元素上调用提供的函数。区别在于map()使用返回值并实际上返回相同大小的新Array。

let query = [
  {
    "id": 40,
    "createdAt": "2018-07-23T10:01:49.939Z",
    "updatedAt": "2018-07-23T10:01:49.939Z",
    "createdBy": 1,
    "updatedBy": null,
    "state": 1,
    "version": 1,
    "requestStatus": 1,
 },
{
    "id": 40,
    "createdAt": "2018-07-23T10:01:49.939Z",
    "updatedAt": "2018-07-23T10:01:49.939Z",
    "createdBy": 1,
    "updatedBy": null,
    "state": 1,
    "version": 1,
    "requestStatus": 1,
},
{
    "id": 40,
    "createdAt": "2018-07-23T10:01:49.939Z",
    "updatedAt": "2018-07-23T10:01:49.939Z",
    "createdBy": 1,
    "updatedBy": null,
    "state": 1,
    "version": 1,
    "requestStatus": 1,
}
];
let startDate = [];
let endDate = [];

 
query.forEach(leaveRequest =>  {
      startDate.push(leaveRequest.createdAt);
      endDate.push(leaveRequest.updatedAt);  
});
console.log(startDate);
console.log(endDate);

答案 1 :(得分:1)

您不需要内部地图,只需这样做,

query.map(leaveRequest =>  {
      startDate.push(leaveRequest.createdAt);
      endDate.push(leaveRequest.updatedAt);  
});

DEMO

let query = [
  {
    "id": 40,
    "createdAt": "2018-07-23T10:01:49.939Z",
    "updatedAt": "2018-07-23T10:01:49.939Z",
    "createdBy": 1,
    "updatedBy": null,
    "state": 1,
    "version": 1,
    "requestStatus": 1,
 },
{
    "id": 40,
    "createdAt": "2018-07-23T10:01:49.939Z",
    "updatedAt": "2018-07-23T10:01:49.939Z",
    "createdBy": 1,
    "updatedBy": null,
    "state": 1,
    "version": 1,
    "requestStatus": 1,
},
{
    "id": 40,
    "createdAt": "2018-07-23T10:01:49.939Z",
    "updatedAt": "2018-07-23T10:01:49.939Z",
    "createdBy": 1,
    "updatedBy": null,
    "state": 1,
    "version": 1,
    "requestStatus": 1,
}
];
let startDate = [];
let endDate = [];

 
query.map(leaveRequest =>  {
      startDate.push(leaveRequest.createdAt);
      endDate.push(leaveRequest.updatedAt);  
});
console.log(startDate);

答案 2 :(得分:0)

All your above answer is almost correct but you may forget the async and await because when I applied those all its properties work as well.

 async functionName() {
    const startDate = [];
    const endDate = [];
    let query;
    query = await this.createQueryBuilder("LeaveRequest")
          .where("LeaveRequest.employeeId = :employeeId", { employeeId })
          .getMany();
    query.leaveRequest.map(function (leaveRequest) {
          leaveRequest.map(function (values) {
          startDate.push(values.startDate);
          endDate.push(values.endDate);
      });
    });
    return {startDate, endDate};
    }