0

I am using Mongoose to update an Announcement record, with the following definition:

const { DateTime } = require('luxon');

const Schema = new mongoose.Schema({
  title: String,
  description: String,
  date: {
    type: Date,
    set: (dt) => dt.toJSDate(),
    get: (d) => DateTime.fromJSDate(d),
  },
  club: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Club',
  },
});

I am performing the update operation in this function

exports.update = async (id, params) => {
  console.log(params)
  await Announcement.findOneAndUpdate({ _id: id }, params, {
    upsert: true,
    useFindAndModify: false,
  });

  return "exports.get(id)";
};

However, I get an error when running Announcement.findOneAndUpdate:

$set' is empty. You must specify a field like so: {$set: {<field>: ...}}

This is what the params look like:

{
  title: 'Name!',
  description: 'Description!',
  date: DateTime {
    ts: 1601524800000,
    _zone: LocalZone {},
    loc: Locale {
      locale: 'en-US',
      numberingSystem: null,
      outputCalendar: null,
      intl: 'en-US',
      weekdaysCache: [Object],
      monthsCache: [Object],
      meridiemCache: null,
      eraCache: {},
      specifiedLocale: null,
      fastNumbersCached: null
    },
    invalid: null,
    weekData: null,
    c: {
      year: 2020,
      month: 10,
      day: 1,
      hour: 0,
      minute: 0,
      second: 0,
      millisecond: 0
    },
    o: -240,
    isLuxonDateTime: true
  },
  club: '99cb91bdc3464f14678934ca'
}

I know that all the ObjectId's are valid because I have tested them from Mongo shell. However, I can't figure out why I am not able to run findOneAndUpdate on an Announcement record.

Pablo
  • 1,302
  • 1
  • 16
  • 35
  • { `$set: params` }. Basically, the second slot of the function is where `$set` goes hence the error. – Minsky Nov 11 '20 at 23:45
  • @Minsky I was following the interface here https://mongoosejs.com/docs/tutorials/findoneandupdate.html – Pablo Nov 12 '20 at 00:42
  • Uhm, that's interesting. I'd try firing an issue in mongoosejs. – Minsky Nov 12 '20 at 01:03
  • check this out https://stackoverflow.com/questions/12636938/set-field-as-empty-for-mongo-object-using-mongoose . Change `null` to `undefined`. But the field you're trying to remove **must be in your Schema**. – Minsky Nov 12 '20 at 01:09
  • @Minsky i tried setting date to just Date() to see if there were any issues caused by DateTime, but still having the same error – Pablo Nov 12 '20 at 01:50

0 Answers0