'GeometryDataType' 类型中不存在坐标

coordinates does not exist in type 'GeometryDataType'

这是 问题的跟进。现在我正确地使用了点对象但是我得到了这个错误:

src/actions/collectGpsData.ts:22:43 - error TS2322: Type '{ type: string; coordinates: any[]; }' is not assignable to type 'GeometryDataType'.
  Object literal may only specify known properties, and '"coordinates"' does not exist in type 'GeometryDataType'.

22       place.location = { "type": "Point", "coordinates": [point.latitude, point.longitude] };
                                             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Found 1 error.

import { Action } from "actionhero"; // No GeometryDataType sequelize.js definition
import { Place } from "../models/Place";

export class CollectGpsData extends Action {
  constructor() {
    super();
    this.name = "collectGpsData";
    this.description = "Collects GPS data and inserts it into the database";
    this.outputExample = {};
    this.inputs = {
      gpsdata: {
        required:true
      }
    }
  }

  async run(data) {
    var GPSdata = data.params.gpsdata;
    GPSdata.forEach(point => {
      var place = new Place();
      place.deviceimei = point.deviceimei;
      place.location = { "type": "Point", "coordinates": [point.latitude, point.longitude] }; //error line
    });
  }
}

为什么会出现此错误?以及我以后如何避免这个错误?

我已经在your GitHub issue中回答了:

Hello @krlicmuhamed!

As @nainkunal933 noted, please include a complete code sample next time. The code you posted does not show how User is defined, for example. Please use a sequelize-sscce.

That said, I took the time to look into what's happening and tried to figure it out.

So, first of all, this issue is typescript-only. The code works fine in runtime. Please include this information directly next time.

I ventured into your Whosebug question and comments and found this:

https://gist.github.com/krlicmuhamed/199c0bc3560a08718b553f3f609acbcd#file-places-ts-L22

Did you find any documentation instructing you to use this specific type explicitly here? If yes, please post the link so I can fix it. Regardless, I don't blame you because we really should have a set of recommended types to apply on each data type. It turns out GeometryDataType is not the correct type to use in this case.

The solution is to install @types/geojson:

  • npm install --save-dev @types/geojson

Then, import Geometry:

import type { Geometry } from '@types/geojson'

And then replace that line of code in which you put GeometryDataType with Geometry:

   @AllowNull(false)
   @Column(DataTypes.GEOMETRY)
-  location: GeometryDataType;
+  location: Geometry;