keystonejs/keystone-classic
View on GitHubHidden TextArray only saves the last element
Open
#4377 opened on Jun 29, 2017
bughelp wanted
Description
I got weird results while working on a model with a TextArray field and found out that the field saves different data depending on whether it's hidden: true or not.
Steps to reproduce the actual/current behavior
I created a model to showcase the problem. Copy & paste it to try it out yourself.
// models/TextArrayBug.js
var _ = require('lodash');
var keystone = require('keystone');
var Types = keystone.Field.Types;
var TextArrayBug = new keystone.List('TextArrayBug');
TextArrayBug.add({
visibleArray: { type: Types.TextArray },
hiddenArray: { type: Types.TextArray, hidden: true }
});
TextArrayBug.schema.pre('save', function (next) {
const newValue = _.uniqueId();
// Both arrays are treated equally / the same value should be set
this.visibleArray = [ ...this.visibleArray, newValue ];
this.hiddenArray = [ ...this.hiddenArray, newValue ];
// Output result:
console.log(this.visibleArray, this.hiddenArray);
next();
});
TextArrayBug.register();
Actual/Current behavior
If you create a new document from that model and save it multiple times, this is the output in the console:
GET /keystone/api/text-array-bugs?fields=id&limit=100&sort=&expandRelationshipFields=true 200 16.074 ms
["1"] ["1"]
POST /keystone/api/text-array-bugs/create 200 605.644 ms
GET /keystone/api/text-array-bugs/595500cfff2a430daf60abc5?drilldown=true 200 5.198 ms
["1","2"] ["2"]
POST /keystone/api/text-array-bugs/595500cfff2a430daf60abc5 200 40.561 ms
["1","2","3"] ["3"]
POST /keystone/api/text-array-bugs/595500cfff2a430daf60abc5 200 19.495 ms
["1","2","3","4"] ["4"]
POST /keystone/api/text-array-bugs/595500cfff2a430daf60abc5 200 9.249 ms
["1","2","3","4","5"] ["5"]
POST /keystone/api/text-array-bugs/595500cfff2a430daf60abc5 200 11.698 ms
["1","2","3","4","5","6"] ["6"]
POST /keystone/api/text-array-bugs/595500cfff2a430daf60abc5 200 11.614 ms
The hidden TextArray only saves the last item from the list while the visible TextArray saves the whole list of items.
Expected behavior
From my understanding, the hidden property shouldn't have any effect on the value of the field. The TextArray should save the whole array of values regardless of whether it's hidden or not.
Environment
| Software | Version |
|---|---|
| Keystone | 4.0.0-beta.5 |
| Node | v8.0.0 |