I'm encountering the following error when trying to post sensor data to my API: MongoServerError: 'timestamp_property' must be present and contain a valid BSON UTC datetime value.
Here's the code for my test:
test("POST /api/sensors/:id/data - Should add new sensor data", async () => {
const fakeId = new mongoose.Types.ObjectId();
const sensorData = {
name: "New Sensor",
property_2: "Room B",
timestamp_property: new Date(), // Valid Date object
metadata_property: { location: "Lab 2", type: "Humidity" },
};
console.log(`New response is ${sensorData.timestamp_property}`);
const response = await request(app)
.post(`/api/sensors/${fakeId}/data`)
.set("Content-Type", "application/json")
.send(sensorData);
expect(response.status).toBe(201);
expect(response.body.task.name).toBe("New Sensor");
});
This is my Mongoose model:
const mongoose = require("mongoose");
const TaskSchema = new mongoose.Schema(
{
name: String,
property_2: String,
timestamp_property: Date, // Should be a Date
metadata_property: Object,
},
{
timeseries: {
timeField: "timestamp_property",
metaField: "metadata_property",
granularity: "hours",
},
}
);
module.exports = mongoose.model("Task", TaskSchema);
I've ensured that timestamp_property is being set to a Date object, but I'm still getting the MongoServerError. Could someone explain what might be causing this issue?
Thanks in advance!
I've ensured that timestamp_property is being set to a Date object, but I'm still getting the MongoServerError.
I'm encountering the following error when trying to post sensor data to my API: MongoServerError: 'timestamp_property' must be present and contain a valid BSON UTC datetime value.
Here's the code for my test:
test("POST /api/sensors/:id/data - Should add new sensor data", async () => {
const fakeId = new mongoose.Types.ObjectId();
const sensorData = {
name: "New Sensor",
property_2: "Room B",
timestamp_property: new Date(), // Valid Date object
metadata_property: { location: "Lab 2", type: "Humidity" },
};
console.log(`New response is ${sensorData.timestamp_property}`);
const response = await request(app)
.post(`/api/sensors/${fakeId}/data`)
.set("Content-Type", "application/json")
.send(sensorData);
expect(response.status).toBe(201);
expect(response.body.task.name).toBe("New Sensor");
});
This is my Mongoose model:
const mongoose = require("mongoose");
const TaskSchema = new mongoose.Schema(
{
name: String,
property_2: String,
timestamp_property: Date, // Should be a Date
metadata_property: Object,
},
{
timeseries: {
timeField: "timestamp_property",
metaField: "metadata_property",
granularity: "hours",
},
}
);
module.exports = mongoose.model("Task", TaskSchema);
I've ensured that timestamp_property is being set to a Date object, but I'm still getting the MongoServerError. Could someone explain what might be causing this issue?
Thanks in advance!
I've ensured that timestamp_property is being set to a Date object, but I'm still getting the MongoServerError.
Share Improve this question asked Jan 29 at 19:25 BobBob 112 bronze badges1 Answer
Reset to default 0I think the structure of your new mongoose.Schema({})
can be improved:
const mongoose = require("mongoose");
const TaskSchema = new mongoose.Schema(
{
name: {
type: String,
required: true //or false
},
property_2: {
type: String,
required: true //or false
},
timestamp_property: {
type: Date,
required: true //or false,
default: Date.now //if you want the current timestamp
}, // Should be a Date
metadata_property: {
type: Object,
required: true //or false
},
// rest of your schema here
}
);
module.exports = mongoose.model("Task", TaskSchema);
You can read more here to further develop your schema.