Suppose we have an error object declared as follows:
const error = new Error('Error');
I attempted to store this error object in a MongoDB field using the Object type, and even tried the Mixed type, but it ends up storing an empty Object. How can I resolve this issue?
const UserLogSchema = new Schema(
{
...
error: {
type: Schema.Types.Mixed
}
},
{
timestamps: true
}
);
const UserLog = mongoose.model('UserLog', UserLogSchema);
Adding the error into the database:
const userLog = await UserLog.findOne({ _id: userLogID });
userLog.error = new Error('Error');
await userLog.save();
Upon attempting to retrieve the error at a later stage:
const userLog = await UserLog.findOne({ _id: userLogID });
console.log(userLog.error)
The console output is {}
, indicating an empty object. However, the original error is not empty.