-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
[interop] Array default with transfrom #8012
Comments
Going to postpone this until 5.7.0 because I think this is a bit too risky for a patch release. |
@vkarpov15 after testing it in mongoose@5.7.0 the initial example i gave, dosnt work, but others do works: import * as mongoose from "mongoose";
const testSchema = new mongoose.Schema({
testArray: [{ type: String, lowercase: true }]
});
const testModel = mongoose.model("test", testSchema);
(async () => {
mongoose.set("debug", true);
await mongoose.connect(`mongodb://localhost:27017/verify`, {
useNewUrlParser: true,
useFindAndModify: true,
useCreateIndex: true,
autoIndex: true
});
const doc = await testModel.create({ testArray: ["MAKE ME LOWERCASE"] });
console.log(doc.toObject());
await mongoose.disconnect();
})();
// result:
// {"testArray":["make me lowercase"],"_id":"5d772503932ef4351940bff4","__v":0} dosnt work: import * as mongoose from "mongoose";
const testSchema = new mongoose.Schema({
testArray: [{ default: ["testDefault"], type: String, lowercase: true }]
});
const testModel = mongoose.model("test", testSchema);
(async () => {
mongoose.set("debug", true);
await mongoose.connect(`mongodb://localhost:27017/verify`, {
useNewUrlParser: true,
useFindAndModify: true,
useCreateIndex: true,
autoIndex: true
});
const doc = await testModel.create({});
console.log(doc.toObject());
await mongoose.disconnect();
})();
// result:
// { testArray: [], _id: 5d77249e9341eb34490df597, __v: 0 } so my assumption is that default dosnt get applied to arrays when defined so PS: sorry for not testing it earlier |
@hasezoey that's because you're defining the default on the individual array values, not on the array as a whole. If you define your schema as shown below, it works fine. const testSchema = new mongoose.Schema({
testArray: {
type: [{ type: String, lowercase: true }],
default: ["testDefault"]
}
}); |
Thanks for implementing it and for the documentation, but now where i try to implement it, i got some question on how to use it, because:
what i more fully mean: // "Type" can be something like "mongoose.Types.String"
// logger is npm package "log-level"
const returnObject = {
type: [{
type: Type
}]
};
// @ts-ignore
if (Type.prototype.OptionsConstructor instanceof mongoose.SchemaTypeOptions) { // returns false
for (const [key, value] of Object.entries(options)) {
if (key in Type.prototype.OptionsConstructor) { // when disabling the "returns false" check above, this always takes the else route
logger.debug('Value is in OC:', key);
returnObject.type[0][key] = value;
} else {
logger.debug('Value is not in OC:', key);
returnObject[key] = value;
}
}
logger.debug('final obj', returnObject);
} am i just using it wrong? @vkarpov15 and from the documentation update: node: 12.11.1 |
> Object.getOwnPropertyDescriptor(mongoose.Schema.Types.String.prototype.OptionsConstructor.prototype, 'trim')
{ value: null,
writable: true,
enumerable: true,
configurable: true }
>
> Object.getOwnPropertyNames(mongoose.Schema.Types.String.prototype.OptionsConstructor.prototype)
[ 'constructor', 'enum', 'match', 'lowercase', 'trim', 'uppercase' ]
> Sorry for the long stream of dots and prototypes. Might be easier to just export |
im fine with it, as long as it is consistent
but -> it seems like it seems the type Update: i made an new issue for this Thanks for this, it made the typegoose code more understandable & maintainable instead of having to redefine the options when mongoose updates :) |
No but |
mongoose version: 5.6.6(NPM)
nodejs: 12.7.0
reproduce script:
log
current behavior: dosnt apply the default value when formatted so / has a "string" transform
wanted behavior: applys default value when formatted so
PS: i make this a question, because i dont know if this is an actual bug
and if there is actually an other issue having this problem, i didnt find it...
The text was updated successfully, but these errors were encountered: