简体   繁体   中英

Mongoose stack error on clone.js while trying to update document

I've been trying to update a mongoDB document using mongoose but I keep getting an error I can't seem to find the end of.

I have details of my problem commented out inside the code.

const { dataModel } = require('../database/ticket'); // dataModel is  my schema
var state = new dataModel({
                _id: _id,
                from: `Me`,
                size: 20,
                subject: "Hello world",
                status : "Working"
});
await state.save();
// Till this point everything works fine

//Now if I try updating stuff inside my state then update the db document is where I have issues.

state.from = 'You';
state.size = 40;
state.status : "Updating";

// For the actual update I've tried multiple methods.

// v1 direct update using the above object
await state.update(); // -> error for updating immutable field _id

//v2 Using a query
dataModel .updateOne({_id:state._id},state); // assumed the issue is trying to force a new _id for the update, so ok

//v3 (v2 without the _id)
const objectWithoutKey = (object, key) => {
    var clone = JSON.parse(JSON.stringify(object));
    delete clone[key];
    return clone;
}
dataModel .updateOne({_id:state._id},objectWithoutKey(state,"_id")); // At this point I can only wonder what's wrong

For v1 I get the following :

D:\somethin\something\node_modules\mongodb\lib\operations\update.js:115
                return callback(new error_1.MongoServerError(res.writeErrors[0]));
                                ^

MongoServerError: Performing an update on the path '_id' would modify the immutable field '_id'
    at D:\somethin\something\node_modules\mongodb\lib\operations\update.js:115:33
    at D:\somethin\something\node_modules\mongodb\lib\cmap\connection_pool.js:273:25
    at handleOperationResult (D:\somethin\something\node_modules\mongodb\lib\sdam\server.js:327:20)
    at Connection.onMessage (D:\somethin\something\node_modules\mongodb\lib\cmap\connection.js:215:9)
    at MessageStream.<anonymous> (D:\somethin\something\node_modules\mongodb\lib\cmap\connection.js:63:60)
    at MessageStream.emit (node:events:527:28)
    at processIncomingData (D:\somethin\something\node_modules\mongodb\lib\cmap\message_stream.js:108:16)
    at MessageStream._write (D:\somethin\something\node_modules\mongodb\lib\cmap\message_stream.js:28:9)
    at writeOrBuffer (node:internal/streams/writable:389:12)
    at _write (node:internal/streams/writable:330:10) {
  index: 0,
  code: 66,
  [Symbol(errorLabels)]: Set(0) {}
}

For v2 & v3 I get the following error despite not explicitly touching the 'clone.js' module.

D:\something\stuff\node_modules\mongoose\lib\helpers\clone.js:29
function clone(obj, options, isArrayChild) {
              ^
RangeError: Maximum call stack size exceeded
    at clone (D:\something\stuff\node_modules\mongoose\lib\helpers\clone.js:29:15)
    at cloneObject (D:\something\stuff\node_modules\mongoose\lib\helpers\clone.js:140:17)
    at clone (D:\something\stuff\node_modules\mongoose\lib\helpers\clone.js:69:16)
    at cloneObject (D:\something\stuff\node_modules\mongoose\lib\helpers\clone.js:140:17)
    at clone (D:\something\stuff\node_modules\mongoose\lib\helpers\clone.js:69:16)
    at cloneObject (D:\something\stuff\node_modules\mongoose\lib\helpers\clone.js:140:17)
    at clone (D:\something\stuff\node_modules\mongoose\lib\helpers\clone.js:69:16)
    at cloneObject (D:\something\stuff\node_modules\mongoose\lib\helpers\clone.js:140:17)
    at clone (D:\something\stuff\node_modules\mongoose\lib\helpers\clone.js:69:16)
    at cloneObject (D:\something\stuff\node_modules\mongoose\lib\helpers\clone.js:140:17)

I'm honestly quite confused. I'm not making any other queries. A last resort solution to this I suppose it could be deleting the document in cause and save it again with the updated data, hoping that will work.

You should just pass the fields that you want to update to the updateOne method:

const { dataModel } = require('../database/ticket'); 

var state = new dataModel({
  _id: _id,
  from: `Me`,
  size: 20,
  subject: 'Hello world',
  status: 'Working',
});

await state.save();

dataModel.updateOne({ _id: state._id }, {
    from: 'You',
    size: 40,
    status: 'Updating'
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM