@@ -18,7 +18,7 @@ function getAllSchemas(req) {
18
18
return req . config . database . adaptiveCollection ( '_SCHEMA' )
19
19
. then ( collection => collection . find ( { } ) )
20
20
. then ( schemas => schemas . map ( Schema . mongoSchemaToSchemaAPIResponse ) )
21
- . then ( schemas => ( { response : { results : schemas } } ) ) ;
21
+ . then ( schemas => ( { response : { results : schemas } } ) ) ;
22
22
}
23
23
24
24
function getOneSchema ( req ) {
@@ -65,7 +65,7 @@ function modifySchema(req) {
65
65
throw new Parse . Error ( Parse . Error . INVALID_CLASS_NAME , `Class ${ req . params . className } does not exist.` ) ;
66
66
}
67
67
68
- let existingFields = Object . assign ( schema . data [ className ] , { _id : className } ) ;
68
+ let existingFields = Object . assign ( schema . data [ className ] , { _id : className } ) ;
69
69
Object . keys ( submittedFields ) . forEach ( name => {
70
70
let field = submittedFields [ name ] ;
71
71
if ( existingFields [ name ] && field . __op !== 'Delete' ) {
@@ -83,24 +83,27 @@ function modifySchema(req) {
83
83
}
84
84
85
85
// Finally we have checked to make sure the request is valid and we can start deleting fields.
86
- // Do all deletions first, then a single save to _SCHEMA collection to handle all additions.
87
- let deletionPromises = [ ] ;
88
- Object . keys ( submittedFields ) . forEach ( submittedFieldName => {
89
- if ( submittedFields [ submittedFieldName ] . __op === 'Delete' ) {
90
- let promise = schema . deleteField ( submittedFieldName , className , req . config . database ) ;
91
- deletionPromises . push ( promise ) ;
86
+ // Do all deletions first, then add fields to avoid duplicate geopoint error.
87
+ let deletePromises = [ ] ;
88
+ let insertedFields = [ ] ;
89
+ Object . keys ( submittedFields ) . forEach ( fieldName => {
90
+ if ( submittedFields [ fieldName ] . __op === 'Delete' ) {
91
+ const promise = schema . deleteField ( fieldName , className , req . config . database ) ;
92
+ deletePromises . push ( promise ) ;
93
+ } else {
94
+ insertedFields . push ( fieldName ) ;
92
95
}
93
96
} ) ;
94
-
95
- return Promise . all ( deletionPromises )
96
- . then ( ( ) => new Promise ( ( resolve , reject ) => {
97
- schema . collection . update ( { _id : className } , mongoObject . result , { w : 1 } , ( err , docs ) => {
98
- if ( err ) {
99
- reject ( err ) ;
100
- }
101
- resolve ( { response : Schema . mongoSchemaToSchemaAPIResponse ( mongoObject . result ) } ) ;
102
- } )
103
- } ) ) ;
97
+ return Promise . all ( deletePromises ) // Delete Everything
98
+ . then ( ( ) => schema . reloadData ( ) ) // Reload our Schema, so we have all the new values
99
+ . then ( ( ) => {
100
+ let promises = insertedFields . map ( fieldName => {
101
+ const mongoType = mongoObject . result [ fieldName ] ;
102
+ return schema . validateField ( className , fieldName , mongoType ) ;
103
+ } ) ;
104
+ return Promise . all ( promises ) ;
105
+ } )
106
+ . then ( ( ) => ( { response : Schema . mongoSchemaToSchemaAPIResponse ( mongoObject . result ) } ) ) ;
104
107
} ) ;
105
108
}
106
109
@@ -140,7 +143,7 @@ function deleteSchema(req) {
140
143
// We've dropped the collection now, so delete the item from _SCHEMA
141
144
// and clear the _Join collections
142
145
return req . config . database . adaptiveCollection ( '_SCHEMA' )
143
- . then ( coll => coll . findOneAndDelete ( { _id : req . params . className } ) )
146
+ . then ( coll => coll . findOneAndDelete ( { _id : req . params . className } ) )
144
147
. then ( document => {
145
148
if ( document === null ) {
146
149
//tried to delete non-existent class
0 commit comments