Can I call js function in rest api?

Hello everyone

I append d3.js(min) by document.querySelector('head').appendChild(scriptNode) in UI Flows block. And when I run

d3.selectAll('span').style('color', function(d, i) {return i % 2 ? 'red' : 'green';}); in rest api( Expose), it will be this error.

Would you tell me the reason and how to fix it?


Solution

Thank you so much

I soluted it by another import js way.

I use RequireScript() to import external js lib, and it can be used in rest api.

mvp_badge
MVP

Hello,

Javascript functions are typically clientside operations - it appears to be the case, since your function seems to evaluate the color of an HTML <span> element in a Screen. However, a REST API will not have this context - they have no notion of Screens or the HTML inside those Screens. REST APIs will typically be exposed or consumed by the server.

Do you think you could share a little more about your use case? What do you want your REST API to do?

Thank you Afonso Carvalho

I just want to run a  object.function() which like d3.js example in rest api, but get Uncaught ReferenceError: d3 is not defined error


mvp_badge
MVP

This happens because an exposed REST API does not have any references to the d3.js library - you've added it to your UI Flow, but your REST API will run independently from your UI. Reading their website, it appears to be a data visualization/charting library. 

Since it is used to manipulate and produce HTML code, there's no practical way for it to be executed inside a stateless REST API. Can you share why this needs to be done in a REST API? I understand that you want to execute it in that context, but if everyone understands your use case, someone might be able to suggest a better way.

Solution

Thank you so much

I soluted it by another import js way.

I use RequireScript() to import external js lib, and it can be used in rest api.

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.