angular2-jsonapi icon indicating copy to clipboard operation
angular2-jsonapi copied to clipboard

saveRecord does not send attributes into payload.

Open chris93983 opened this issue 7 years ago • 3 comments

environment: I am using angular-jsonapi 4.0.2 with angular 5.1.1. scenario: I am trying to send a post request with dynamic url.

I have a data model like this: @JsonApiModelConfig({ type: "workflowruns" }) export class WorkflowRunModel extends JsonApiModel { @Attribute() producer_id: number; @Attribute() created_at: string; @Attribute() status: string; @Attribute() inputs: { [key: string]: WorkflowRunModuleSpecificationModel; } = {}; }

I send request like this: const result = await this.datastore.saveRecord(WorkflowRunModel, run, null, null, /api/module_manager/workflows/${id}/run).toPromise();

The request post was sent with body below: { "data": { "type": "workflowruns", "attributes": {} } }.

But the actual model I passed is: WorkflowRunModel {_datastore: DataStore, id: undefined, _inputs: {…}, Symbol(AttributeMetadata): {…}} created_at : undefined hasDirtyAttributes : true id : undefined inputs : Object copy_from_url : WorkflowRunModuleSpecificationModel {src_url: ""} trace_neurites : WorkflowRunModuleSpecificationModel {NEURON_PREFIX: "", NUCLEI_PREFIX: "", LOWER_THRESH_NEURON: 41, LOWER_THRESH_NUCLEI: 40, MIN_NUCLEI_SIZE: "", …} unzip : WorkflowRunModuleSpecificationModel {} __proto__ : Object modelConfig : Object producer_id : undefined status : undefined

I also tried to override function "getDirtyAttributes", it accetps only metadata but no model information so I cannot get attributes of the model to be posted.

How can I get this working? Or did I do something wrong?

chris93983 avatar Dec 21 '17 13:12 chris93983

@chris93983 you have the method signatures confused. saveRecord is defined as

JsonApiDatastore.prototype.saveRecord = function (attributesMetadata, model, params, headers, customUrl) {

so you should be calling something like this:

params = { foo: bar }
this.datastore.saveRecord(null, WorkflowRunModel, params, null, /api/module_manager/workflows/${id}/run)

jacobbullock avatar Jan 04 '18 12:01 jacobbullock

@jacobbullock Thanks for the reply. Based on your comment, I tried to do this: const result = await this.datastore.saveRecord(null, WorkflowRunModel, run, null, /api/module_manager/workflows/${id}/run).toPromise(); And I got warning below:

Argument of type 'typeof WorkflowRunModel' is not assignable to parameter of type 'JsonApiModel'. Property '_datastore' is missing in type 'typeof WorkflowRunModel'.

I checked corresponding typescript declaration and it looks like this: saveRecord<T extends JsonApiModel>(attributesMetadata: any, model: T, params?: any, headers?: Headers, customUrl?: string): Observable<T>; So I guess model is the actual entity that we need to pass?

chris93983 avatar Jan 08 '18 03:01 chris93983

Indeed, It is preferred to use the save() Method on the Model itself. Is there any reason you are using saveRecord directly?

HennerM avatar Feb 11 '18 20:02 HennerM