How to read file input from 'Code by Zapier' (Javascript) - javascript

I'm fetching files from a Zapier trigger (with a BrickFTP app).
I'm then invoking the code app (in Javascript) to process the file (parse CSV), using the 'File' attribute of the BrickFTP app as input variable to the code step but cannot successfully read the file content.
The Code step is pretty trivial:
console.log(`Got file content: ${fileContent}`);
output = {
file: fileContent,
}
When I test the code step, I'm getting this error:
Bargle. We hit an error creating a run javascript. :-( Error:
[LazyFile] (3587 bytes) https://zapier.com/engine/hydrate/691575/.eJwtkFFrwjAUhf_KuM9i1mq3WRhjohSERjaqw76ULL2apG3aJVFR6X9fKr4evnu-w72B1NYxzbGQJcRh9BZFs2A6gr3Euiw0axBiKPFU7GWNMAIukFdFhReIX2ZB9Br5qNUOtSvcpRtg6qnqzMzBQnyDo6l9JpzrbEwI67rxr5G82rtuzNvGB5IYtI6cAjIYLCFSE-eTMbenD8adbPW7wVIa5M43PySPNQ065i39CAz-Hf3VoBTISjR3--fRidbIKxtq_NmcWcmf0uthQputyrPlOVfLyU5tAqpok_5spnSxklTNZZ7x53Um1DrZql0mGprsLrlKPT-vafItUrUSNPyaeFakYRqtF90U-vsi0fpPQrLMoO_7f_ftdsQ:1do4Qj:pEkOihXff9eAzu8eLyTpN378vgI/ is not JSON serializable
Screenshots:
The test data
Test results

I had the same problem, and that was because the field type I was assigning was text rather than file. First, you have to use CLI version of Zapier, and then if a field is supposed to accept files, it must be of type 'file'.
In this way, the url of the file is being returned and can be downloaded.

Related

How can I check if my PDF file is "broken" with javascript(React)?

I'm trying to validate my PDF file with a input file, I want some kind of validation, if I send a broken or corrupted PDF, the input file will throw a error or something. How can I do it?
I'm using Next JS.
I checked the object from the broken PDF to see if there's something different and I can't see anything wrong with it, I installed some libs like "is-pdf-valid", but it doesn't work.

how to read text file in folder with javascript

I have a folder structure like this
index.html
file.txt
in the index.html I have a script that wants to read the file.txt and assign it to a variable, say x.
async function loadFile(file) {
let text = await file.text();
console.log(text);
return text
}
var x = loadFile("file.txt")
but this returns an error
main.js:35
Uncaught (in promise) TypeError: file.text is not a function
at loadFile (main.js:35:27)
at main.js:39:13
what to do? I just want to assign file.txt content to a variable.
JavaScript in browser doesn't give you direct access to file system.
What you did is just pass a string to your function and call text() on it. Sadly it doesn't work like that.
Here are two workarounds I know of:
1. fetch api
If you have an http server serving your files then you make a request using fetch() to obtain its content in JS. Read the docs here :
https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
Note that it won't work on pages loaded with file:// URI scheme. You need http(s). If you have python installed run python -m http.server 8000 in directory containing files to be served.
2. FileReader
If the file is to be selected by user as input to a form then you can use FileReader
Have a look these links:
webdev has great explaination for this
https://web.dev/read-files/
also see the docs
https://developer.mozilla.org/en-US/docs/Web/API/FileReader

Original Code Not Executing After Removing Javascript in Eclipse

I tried inserting some javascript into a jsp file on Eclipse. I added a scripts folder to the WebContent folder and added a helloWorld.js file and then added a script tag and did some input validation, which failed to work.
After removing all this failed javascript validation my original code no longer runs. I get a 500 error just trying to submit a parameter on the request. The parameter is null when I enter a number on the form. Any idea why? This is Eclipse.
I have deleted the javascript, the javascript file, and the scripts folder.
Yours Truly
(As Humanly Possible)
Cnight62

How to exit javascript / node.js inside HTML

I have json file ( I create this json file using node.js) that I read in HTML using json2html library and if the json file is present / readble then it will be uploaded in our server.
I have json2html code that reads json file and prints HTML page based on content of json file. If json file is unreadable or not present, then I want node.js / javascript to stop or exit the process so nothing gets uploaded to server. How shall I stop / exit the code from processing further if json file is not available.
I do have process.exit() in my HTML file where I read json file . It does not exit gracefully. When I do inspect elements, it errors Uncaught (in promise) ReferenceError: process is not defined. I do have npm i process even though process is global. I still explicitly define const process = require('process'); in my index.js file. Still I get error while opening my html file in browser. How to get past this error. Please refer below for my code from my html file
let responseContent = fetch('studentRecords.json')
.then(function(response) {
return response.json();
})
.catch(function(err) {
process.exit(1);
});
The problem here is that you are trying to reference process which is a node global variable from the browser in your HTML file. Because of this, it won't exist.
Based on what you've mentioned, you can leave the code block as is and instead of running process.exit(1) you could try logging out an error or wrapping it in a callback function and returning an error instead? That way it won't try to process the fetch response and it will have the option to continue in the way you see fit.

How to read file without input in javascript?

My folder hierarchy is as follows:
a/a.XML
a/b/b.XML
i will select a.XML file and i have read this file. Now i have path of b.XML file. My question is how to read this XML file as i am not getting its input i only have its path. I have searcg on net but i didnt found any help how to read XML file wihout giving its input. I want my code to be run in google chrome
My code for reading XML file throug input is as follows:
if(filePath.files && filePath.files[0])
{
reader.onload = function (e)
{
output = e.target.result;
console.log("file path");
console.log(output);
}
}
The below links should explain you clearly how to read XML using JavaScript.
However, if you are trying to access multiple elements repeatedly, you may also try this...Convert your XML to JSON format (You will find many articles on the Internet on how to convert XML to JSON programatically). JSON can be very easily parsed using JavaScript.It reduces the number of DOM calls you make to reference a particular element and hence it is faster.
http://www.w3schools.com/XML/tryit.asp?filename=tryxml_parsertest
http://www.codetoad.com/xml_javascripti_tutorial.asp
http://www.w3schools.com/XML/xml_examples.asp
http://www.peachpit.com/articles/article.aspx?p=29307&seqNum=4
Check the above Link
From your comment, it seems you're trying to read the xml file from you local system. Which is impossible to do. Following are the error you will suffer
In chrome it returns 404 error.
In FF it retuns 0 [Exception... "Access to restricted URI denied"
code: "1012" nsresult: "0x805303f4 (NS_ERROR_DOM_BAD_URI)".
So first host your file in some server and then load it.

Categories

Resources