Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
219 views
in Technique[技术] by (71.8m points)

node.js - Mongoose: Error using projection when timestamps are enabled

I'm having an issue using projection, particularly the $unset field. I also had the same issue with $pull with arrays. It seems like mongoose uses power-assign under the hood for async for-each operations.

export type IUserModel = Model<IUserDocument>

const UserSchema = new Schema<IUserDocument>({
  email: { type: String },
  meta: {
    created: String,
    updated: String,
  },
  photos: {
    type: Map,
    of: {
      link: String,
      size: Number,
    }
  },
// }, {timestamps: {createdAt: 'meta.created', updatedAt: 'meta.updated' } });
}, { timestamps: true });

export const User = model<IUserDocument, IUserModel>('users', UserSchema);

const removePhoto = async (id, key) => {
  try {
    const query = await User.updateOne({ _id: '' }, { $unset: { [`photos.${key}`]: true } })
    console.log(query)
  } catch(err) {
    console.error(err)
  }
}

I receive the following error:

*** uncaughtException: Error: Invalid operator: "updatedAt"
    at Function.assignUpdateOperation (F:DESKTOPPROJECTSserver
ode_modulespower-assigndistpower-assign.js:125:21)
    at F:DESKTOPPROJECTSserver
ode_modulespower-assigndistpower-assign.js:535:24
    at Array.reduce (<anonymous>)
    at assign (F:DESKTOPPROJECTSserver
ode_modulespower-assigndistpower-assign.js:534:15)
    at saveDiffHistory (F:DESKTOPPROJECTSserver
ode_modulesmongoose-audit-trailindex.js:106:5)
    at F:DESKTOPPROJECTSserver
ode_modulesmongoose-audit-trailindex.js:118:26
    at handleNextResult (F:DESKTOPPROJECTSserver
ode_modulesmongooselibhelperscursoreachAsync.js:86:21)
    at F:DESKTOPPROJECTSserver
ode_modulesmongooselibhelperscursoreachAsync.js:69:9
    at F:DESKTOPPROJECTSserver
ode_modulesmongooselibcursorQueryCursor.js:460:7
    at F:DESKTOPPROJECTSserver
ode_moduleskareemindex.js:136:16

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...