Skip to main content

I am surprised about how you need to set the patch items of an update for making the update runs without error.

I write a dataconnector for retrieving document list and  Initially I used this code for the key to the patch without success:

itemsPatch.set(
apiDocument.documentId, convertToDocument(apiDocument as ApiDocument),
)

Full code:

export const getDocumentListAction = async (action: DataConnectorAsynchronousAction) => {
const apiDocuments = await searchDocuments(action.context.userCredential);
const itemsPatch = new Map<string, SerializedFields>();
var i = 0;
apiDocuments?.forEach((apiDocument) => {
itemsPatch.set(
apiDocument.documentId, convertToDocument(apiDocument as ApiDocument),
);
});
if (apiDocuments) {
action.client
.update({
dataSourceName: "Lucid",
collections: {
"Lucid Documents": {
patch: {
items: itemsPatch,
},
schema: documentSchema,
},
},
})
.catch((error) => {
console.error(
"Error encountered when updating Lucid Documents of Lucid collections",
error
);
});
}
return {
success: true,
};
};

After several days of searching why it was not working, I used

this in adding quote to the key which, for me, seems strange as apiDocument. documentId is already a string:

 

itemsPatch.set(
"\"" + apiDocument.documentId + "\"", convertToDocument(apiDocument as ApiDocument),
)

Full code

export const getDocumentListAction = async (action: DataConnectorAsynchronousAction) => {
const apiDocuments = await searchDocuments(action.context.userCredential);
const itemsPatch = new Map<string, SerializedFields>();
var i = 0;
apiDocuments?.forEach((apiDocument) => {
itemsPatch.set(
"\"" + apiDocument.documentId + "\"", convertToDocument(apiDocument as ApiDocument),
);
});
if (apiDocuments) {
action.client
.update({
dataSourceName: "Lucid",
collections: {
"Lucid Documents": {
patch: {
items: itemsPatch,
},
schema: documentSchema,
},
},
})
.catch((error) => {
console.error(
"Error encountered when updating Lucid Documents of Lucid collections",
error
);
});
}
return {
success: true,
};
};

Any explanation?

Regards.

Hello Michel!

My best guess as to why this would be happening is because an ItemsPatch is not a map, but rather an object that contains a field items that is of type Map<string, SerializedFields>. If you change your variable itemsPatch to match this design, do you still encounter problems?


Hello ​@Connor B,

My variable itemsPatch match this design.

One more thing, that could perhaps help you to figure out what is going on, is that using this same design and if the field of the key is a number it works with the simple underneath syntax.

itemsPatch.set( apiFolder.id.toString(), convertToFolder(apiFolder as ApiFolder));

As you can see It is not necessary in this case to add quotes.

Any further explanation?

Regards


Reply