如何使用nestjs存储JSON特性集合?

huangapple go评论88阅读模式
英文:

How to store a JSON Feature Collection with nestjs?

问题

以下是您提供的内容的中文翻译:

我正在使用nestjs和node-geocoder模块进行开发,并希望将地理编码器接收到的数据存储在我的数据库中。
以下是我在拦截器中的操作(假设所有声明都正确):

var options = {
  provider: "opendatafrance",
};
var geocoder = NodeGeocoder(options);

@Injectable()
export class PointsInterceptor implements NestInterceptor {
  intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
    var monaddress = context.switchToHttp().getRequest().body

    // 使用Promise
    geocoder.geocode(monaddress)
      .then(function (res) {
        console.log('res: ' + res, 0);
        return next.handle().pipe(map(data => ({ res })))
      })
      .catch(function (err) {
        console.log(err);
        return next.handle().pipe(map(data => ({ err })))
      });
    return next.handle();
  }
}

控制器

export class PointsController {
  constructor(private readonly pointsService: PointsService) {}

  @Post()
  @UseInterceptors(PointsInterceptor)
  create(@Body() createPointDto: CreatePointDto) {
    return this.pointsService.create(createPointDto);
  }

  @Get()
  findAll() {
    return this.pointsService.findAll();
  }
}

DTO

export class CreatePointDto {
  readonly address: string;
  readonly type: string;
  readonly coordinates: Enumerator;
}

Geocoder函数的结果是:

如何使用nestjs存储JSON特性集合?

有人知道我如何存储这些数据吗?

非常感谢您的提前帮助。

我使用nestjs cli生成了我的模型以及所有相关内容:

Points Schema

export const PointSchema = new mongoose.Schema({
  address: String,
  type: {
    type: String,
    enum: ['Point'],
    default: 'Point',
    required: false,
  },
  coordinates: {
    type: [Number],
    required: false,
  },
});
英文:

I am playing with nestjs and node-geocoder module and would like to store in my database the data received for the geocoder.
Here is what I did in the Interceptor (Let suppose all the declarations are ok):

provider: &quot;opendatafrance&quot;,
};
var geocoder = NodeGeocoder(options);


@Injectable()
export  class PointsInterceptor implements NestInterceptor {
      intercept(context: ExecutionContext, next: CallHandler): Observable&lt;any&gt; {

var monaddress = context.switchToHttp().getRequest().body

//Using Promise
geocoder.geocode(monaddress)
  .then(function(res) {
    console.log(&#39;res: &#39; + res,0);
    return next.handle().pipe(map(data =&gt; ({ res })))
  })
  .catch(function(err) {
    console.log(err);
    return next.handle().pipe(map(data =&gt; ({ err })))
    
  })  ;
  //console.log(&#39;result: &#39;+result);
 return next.handle()
}
}

The controller::

export class PointsController {
  constructor(private readonly pointsService: PointsService) {}

  @Post()
  @UseInterceptors(PointsInterceptor)
  create(@Body() createPointDto: CreatePointDto) {
     return this.pointsService.create(createPointDto);
  }

  @Get()
  findAll() {
    return this.pointsService.findAll();
  }
}

The dto

export class CreatePointDto {
    readonly address: string;
    readonly type: string;
    readonly coordinates: Enumerator;
}

Res (the result of the geocoding function is):
如何使用nestjs存储JSON特性集合?

Does anyone have an idea on how I can store this data?

Many thanks in advance

I used nestjs cli to generate my model and all that relates:

Points Schema:

export const PointSchema = new mongoose.Schema({
  address: String,
  type: {
    type: String,
    enum: [&#39;Point&#39;],
    default: &#39;Point&#39;,
    required: false,
},
coordinates: {
    type: [Number],
    required: false,
},



});

答案1

得分: 1

为了将地理编码数据存储在您的数据库中,您可以修改PointsService以处理数据的保存。首先,请确保您的NestJS应用程序中已经设置了数据库模块和服务。对于此示例,让我们假设您正在使用MongoDB数据库,并使用Mongoose作为ODM(对象文档映射器)。

  1. 创建一个Point模型,代表您的地理编码点的数据结构。在您的points.model.ts文件中:

  2. 创建一个PointsService,用于与数据库交互以保存地理编码点。在您的points.service.ts文件中:

  3. 修改您的PointsInterceptor,在调用next.handle()方法之前将地理编码数据保存到数据库中。在您的points.interceptor.ts文件中:

希望这对您有所帮助!

英文:

To store the geocoded data in your database, you can modify the PointsService to handle the saving of the data. First, ensure you have a database module and service set up in your NestJS application. For this example, let's assume you are using a MongoDB database with Mongoose as the ODM (Object Document Mapper).

1.Create a Point model that represents the data structure of your geocoded points. In your points.model.ts file:

2.Create a PointsService that interacts with the database to save the geocoded points. In your points.service.ts file:

3.Modify your PointsInterceptor to save the geocoded data to the database before calling the next.handle() method. In your points.interceptor.ts file:

Hope that works!!!

huangapple
  • 本文由 发表于 2023年7月20日 21:08:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76730256.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定