Using modified mongoose-uuid2 breaks save operation

喜欢而已 提交于 2019-12-23 02:46:16

问题


So, I have been battling a problem with nested UUID's in mongoose that is outlined in the following stack overflow post

Mongoose and array of ref UUID's does not convert

However, after some more testing and research, I found that this breaks another part of my code for updates to documents that I use to apply to the system. Since this code is complex, I will provide an simpler example that creates this same condition.

Upon installing the following nodejs packages

"mongoose": "^5.6.10", "mongoose-uuid2": "^2.3.0", "uuid": "^3.3.3", "uuid-mongodb": "^2.1.1"

Then, updating the mongoose-uuid2 index.js file as outlined in the previous post.

and finally setting up the following code that demonstrates the problem:

var mongoose = require('mongoose');
var uuid = require("uuid-mongodb");
var Schema = mongoose.Schema;
require('mongoose-uuid2')(mongoose);
var UUID = mongoose.Types.UUID;

var TestISchema = Schema({
  _id: { type: UUID, default: uuid.v4, required: true },
  name: { type: String, required: true }
}, { id: false, toObject: {
  "getters": true,
  "virtuals": true,
  "transform": function(doc, ret, options) {
    delete ret._id;
    return ret;
  }
}, toJSON: {
  "getters": true,
  "virtuals": true,
  "transform": function(doc, ret, options) {
    delete ret._id;
    return ret;
  }
} });

var TestIISchema = Schema({
  _id: { type: UUID, default: uuid.v4, required: true },
  name: { type: String, required: true },
  testIs: [{ type: UUID, ref: 'Product' }]
}, { id: false, toObject: {
  "getters": true,
  "virtuals": true,
  "transform": function(doc, ret, options) {
    delete ret._id;
    return ret;
  }
}, toJSON: {
  "getters": true,
  "virtuals": true,
  "transform": function(doc, ret, options) {
    delete ret._id;
    return ret;
  }
} });

TestIISchema.virtual("id").get(function() {
  return uuidv4.from(this._id).toString();
});

TestIISchema.virtual("id").set(function(uuid_string) {
  this._id = uuid.from(uuid_string);
});

TestISchema.virtual("id").get(function() {
  return uuid.from(this._id).toString();
});

TestISchema.virtual("id").set(function(uuid_string) {
  this._id = uuidv4.from(uuid_string);
});

mongoose.connect('mongodb://localhost:27017/test', {useNewUrlParser: true});

var TestIModel = mongoose.model('TestI', TestISchema);
var TestIIModel = mongoose.model('TestII', TestISchema);

console.log(`Creating a new Test I document`);
var mytestI = new TestIModel({ "name": "testproduct"});

console.log(`Saving a Test I document`);
mytestI.save().then(() => {
  console.log(`Getting all of Test I documents`);
  return TestIModel.find({});
})
.then((results) => { 
  console.log(`Assigning Test I documennt ID's to an Test II document`);
  var testIuuids = [];
  results.forEach((result) => {
    testIuuids.push(result.id);
  });
  mytestII = new TestIIModel(
    { 
      "name": "testIIproduct", 
      "testIs": testIuuids 
    }
  );
  return Promise.resolve(mytestII);
})
.then((doc) => {
  console.log(`Saving Test II`);
  return doc.save();
})
.then((result) => {
  console.log(result);
  console.log(`Getting Test II by id ${result.id}`);
  return TestIIModel.findById(result.id);
})
.then((document) => {
  document.name = "testIIproduct2";
  document.testIs = []
  return document.save();
})
.then((result) => {
  console.log(result);
})
.catch((error) =>{
  console.log(error);
  mongoose.disconnect();
});

Upon installing and executing the following error occurs on the last save.

Path _id is required.

Upon troubleshooting and running it through the debugger I can see the code going through the updated patched code and if I remove the updated code that is outlined in the previous post for mongoose-uuids it works. However, my previous problem that is outlined in that post comes back. I also tried removing the "required: true" on line 8 and 27 on the schema, and this produced another error on line 96 findById:

No document found for query "{ _id: \'eacdf837-ee2c-4c61-9271-744234b67868\' }

I also tried changing line 96 findById to TestIIModel.find({"id": result.id}), however the Model could not find the document.

My apologies for the large post but I could not find a way to recreate this issue without all of this information. Also as a background, using UUID's in this manner is a requirement. I am stumped by this, so any assistance in this matter would be appreciated.


回答1:


I was able to find a solution for this problem. The answer was posted on

Mongoose and array of ref UUID's does not convert

It is a modification to the the mongoose-uuid2 library package.

It is a change to line 51 to 57

if (value instanceof mongoose.Types.Buffer.Binary) {
    if (init && doc instanceof mongoose.Types.Embedded) {
      return getter(value);
    } else {
      return value;
    }
  }


来源:https://stackoverflow.com/questions/57641291/using-modified-mongoose-uuid2-breaks-save-operation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!