Skip to content

Commit db2661f

Browse files
authored
Fix/data formatting (#13)
* fix(formatting): Fix data formatting and nan values * fix(formatting): Fix data formatting and nan values
1 parent 56953e8 commit db2661f

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

index.js

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,33 @@ module.exports = function (app) {
5656
source: update['$source'],
5757
context: delta.context,
5858
path: val.path,
59-
time: update.timestamp,
59+
time: new Date(update.timestamp), // Ensure time is stored as Date
60+
uid: this.generateUID(JSON.stringify(val)), // Generate UID from the value
6061
};
6162

63+
// Determine the type of value and handle accordingly
6264
if (val.path === 'navigation.position') {
63-
payload.value = {
64-
type: 'Point',
65-
coordinates: [val.value.longitude, val.value.latitude],
66-
};
67-
} else {
65+
// Ensure GeoJSON format for navigation.position
66+
if (typeof val.value === 'object' && val.value.latitude && val.value.longitude) {
67+
payload.value = {
68+
type: 'Point',
69+
coordinates: [val.value.longitude, val.value.latitude],
70+
};
71+
} else {
72+
// Handle invalid or unexpected structures
73+
app.error(`Invalid position value: ${JSON.stringify(val.value)}`);
74+
return;
75+
}
76+
} else if (typeof val.value === 'number' || typeof val.value === 'string') {
77+
// Store numbers and strings directly
78+
payload.value = val.value;
79+
} else if (typeof val.value === 'object') {
80+
// Store JSON objects directly
6881
payload.value = val.value;
82+
} else {
83+
// Handle unexpected types (e.g., undefined, NaN)
84+
app.error(`Unexpected value type: ${typeof val.value} for path: ${val.path}`);
85+
return;
6986
}
7087

7188
options.defaultTags.forEach(tag => {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "signalk-to-mongodb",
3-
"version": "2.2.0",
3+
"version": "2.2.1",
44
"description": "SignalK plugin to store data to cloud hosted MongoDB URI. It provides an easy integration of SignalK server with MongoDB to log and store boat data in real-time.",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)