mirror of
https://github.com/tdurieux/anonymous_github.git
synced 2026-05-15 22:48:00 +02:00
fix(gist): set gist subpaths individually to avoid CastError
Mongoose treats `gist` as a nested path, not a sub-schema, so
set("gist", payload) mis-casts the inner subdoc arrays and fails
validation with 'Cast to [string] failed' at gist.files.0. Set each
subpath individually so the files/comments arrays cast correctly.
This commit is contained in:
+10
-5
@@ -99,10 +99,6 @@ export default class Gist {
|
||||
author: comment.user?.login || "",
|
||||
}));
|
||||
|
||||
// Mongoose treats `gist` as a nested path; assigning a plain object that
|
||||
// contains nested arrays (files/comments) on an unsaved doc silently drops
|
||||
// those arrays. Cache the populated payload off-model so toJSON can read
|
||||
// it directly, and also set it on the model for the persisted path.
|
||||
const payload = {
|
||||
description: gistInfo.data.description || "",
|
||||
isPublic: gistInfo.data.public,
|
||||
@@ -117,7 +113,16 @@ export default class Gist {
|
||||
comments: commentsMapped,
|
||||
};
|
||||
this._gistPayload = payload;
|
||||
this._model.set("gist", payload);
|
||||
// `gist` is a nested path (not a sub-schema), so assigning the whole
|
||||
// object via set("gist", payload) mis-casts the inner subdoc arrays.
|
||||
// Set each sub-path individually so Mongoose casts arrays correctly.
|
||||
this._model.set("gist.description", payload.description);
|
||||
this._model.set("gist.isPublic", payload.isPublic);
|
||||
this._model.set("gist.creationDate", payload.creationDate);
|
||||
this._model.set("gist.updatedDate", payload.updatedDate);
|
||||
this._model.set("gist.ownerLogin", payload.ownerLogin);
|
||||
this._model.set("gist.files", files);
|
||||
this._model.set("gist.comments", commentsMapped);
|
||||
this._model.markModified("gist");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user