最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - MongoServerError: 'timestamp_property' must be present and contain a valid BSON UTC datetime value

programmeradmin1浏览0评论

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 badges
Add a comment  | 

1 Answer 1

Reset to default 0

I 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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论