node js – Encountering an error: “Unexpected end of JSON input” when inserting geojson data into MongoDB
In my MERN application, I am getting an
Unexpected end of JSON input
when trying to insert a geojson data in my MongoDB using Thunder client in Visual Studio. Here is my geojson data:
{
"type": "Feature",
"properties": {
"region": "Region I",
"province": "Ilocos Norte",
"municName": "Adams",
"municCode": "PH012801000",
"municID": 1,
"aveBU": [],
"aveDC": [],
"aveDMC": [],
"aveFFMC": [],
"aveFWI": [
{
"year": 2020,
"week": 1,
"value": 0.0010034
},
{
"year": 2020,
"week": 2,
"value": 0.0010034
},
{
"year": 2020,
"week": 3,
"value": 0.0010034
},
{
"year": 2020,
"week": 4,
"value": 0.0010034
},
{
"year": 2020,
"week": 5,
"value": 0.0010034
}
],
"aveISI": [],
"aveRain": [],
"aveRH": [],
"aveTemp": [
{
"year": 2020,
"week": 1,
"value": 0.5339749
},
{
"year": 2020,
"week": 2,
"value": 0.5339749
},
{
"year": 2020,
"week": 3,
"value": 0.5339749
},
{
"year": 2020,
"week": 4,
"value": 0.5339749
},
{
"year": 2020,
"week": 5,
"value": 0.5339749
}
],
"aveWSpeed": []
},
"geometry": {
"type": "MultiPolygon",
"coordinates": [
[
[
[
120.94836326655161,
18.381693430307621
],
[
120.947043606777129,
18.420625793045986
],
[
120.958673098085114,
18.463202411961788
],
[
120.969145182604166,
18.510116371173638
],
[
120.946256721570535,
18.511575596736463
],
[
120.920678030366844,
18.514622550190609
],
[
120.921545641418334,
18.518382190285877
],
[
120.872670318380528,
18.509416894674018
],
[
120.889293471616497,
18.476307956047378
],
[
120.870356694605789,
18.429886047403215
],
[
120.914840178431518,
18.376933021042134
],
[
120.94836326655161,
18.381693430307621
]
]
]
]
}
}
Here is my model/schema FireIndexModel.js:
import mongoose from "mongoose";
const indexDataSchema = mongoose.Schema({
year: Number,
week: Number,
value: Number
});
const fireIndexSchema = mongoose.Schema({
type: {
type: String,
enum: ['Feature'],
required: true,
},
properties: {
region: String,
province: String,
municName: String,
municCode: String,
municID: Number,
aveBU: [indexDataSchema],
aveDC: [indexDataSchema],
aveDMC: [indexDataSchema],
aveFFMC: [indexDataSchema],
aveFWI: [indexDataSchema],
aveISI: [indexDataSchema],
aveRain: [indexDataSchema],
aveRH: [indexDataSchema],
aveTemp: [indexDataSchema],
aveWSpeed: [indexDataSchema]
},
geometry: {
type: {
type: String,
enum: ['MultiPolygon'],
required: true,
},
coordinates: {
type: [[[Number]]],
required: true,
},
}
});
export const FireIndex = mongoose.model('FireIndex', fireIndexSchema);
Here is my FireIndexController.js where I try to insert the data:
import { FireIndex } from "../models/fireIndexModel.js";
// @desc Add Fire Index data
// @route POST /api/fire_index/
// @access Private
const addFireIndex = async(req, res) => {
try {
console.log("Received json: ", req.body );
const fireIndex = new FireIndex(req.body);
fireIndex.geometry.coordinates = JSON.parse(fireIndex.geometry.coordinates);
await fireIndex.save();
res.json({ message: 'Fire Index created' });
} catch (error) {
//console.log(error);
res.status(500).json({error: error.message})
}
}
// @desc Get all Fire Indices
// @route GET /api/fire_index/
// @access Private
const getFireIndices = async(req, res) => {
try {
const fireIndex = await FireIndex.find();
res.json(fireIndex);
} catch (error) {
//console.log(error);
res.status(500).json({error: error.message})
}
}
export {
addFireIndex,
getFireIndices
}
I have checked my data using geojsonlint.com and there is no error. How should I correctly read my geojson data? I still have not created a form using React.js as I am testing first my backend express server. On a side note, is my model correct?
Read more here: Source link
