Utilizing my own API, I am fetching data using the following code:
fetch('/guidebook/api/peak-data/')
.then(response => response.json())
.then(response => JSON.stringify((response)))
.then(data => {
console.log('Raw JSON data:', data);
console.log('Type of data:', typeof data);
console.log('Type of features:', typeof data.features);
})
.catch(error => {
console.log('Error:', error);
});
The data is retrieved from this function and is accessible in api/peak-data
urlpatterns = [
path('', views.returnPage),
path('api/peak-data/', views.get_peak_data, name='peak_data'),
]
def get_peak_data(request):
peaks = Peak.objects.all()
peak_data = serialize('geojson', peaks, geometry_field='peak_coordinates')
return JsonResponse(peak_data, safe=False, content_type='application/json')
This section provides information on the corresponding django model:
class Peak(models.Model):
peak_id = models.BigIntegerField()
peak_name = models.CharField(max_length=80)
peak_coordinates = postgismodel.PointField(geography=True, default=Point(0, 0), blank=True, null = True)
peak_elevation = models.IntegerField()
When converting the data to JSON format, it unexpectedly results in a string representation
Type of data: string
Type of features: undefined
No features found in the JSON data.
I am facing challenges in comprehending why this occurs. Specifically, I aim to extract the coordinates attributes utilizing Json, but the current output impedes this process.