Need explanation on structure of Object in Node.js - javascript

I did a console.log of some parameter that is available to a callback function in the Node.js formidable package. I am using this package for processing file uploads. The output of the console.log(files) is:
{
fileUpload: [
PersistentFile {
_events: [Object: null prototype],
_eventsCount: 1,
_maxListeners: undefined,
lastModifiedDate: 2023-02-16T19:45:33.257Z,
filepath: '/home/uploads/someName',
newFilename: '8655b57ce96e3b679ffdd7e00',
originalFilename: 'anotherFile.jpg',
mimetype: 'image/jpeg',
hashAlgorithm: false,
size: 7,
_writeStream: [WriteStream],
hash: null,
[Symbol(kCapture)]: false
}
]
}
I am expecting an object with each key('fileUpload', for example) having as value an array of objects. I am unable to understand the role of PersistentFile in the object's structure. I think that it breaks the conventional structure of an object. It gives a syntax error if I try to copy/paste it to a variable.
How can I process this data structure (or object, or whatever...) in Javascript?
EDIT:
My need is to get only properties from this object which have scalar values only: lastModifiedDate, filepath, originalFilename, mimetype, and size. So the resultant object will be:
{
fileUpload: [
{
originalFilename:
filepath:...
...
},
{
originalFilename:
filepath:...
...
}
],
fileField2: [
{...},
{...}
...
],
...
}

This object contains a WriteStream object which is not serializable in JSON because it has a live file handle in it. You could copy select properties out of this into a new object and then serialize that using JSON.stringify().

Related

How do I extract the Blob and the ArrayBuffer / TypedArray from the Formidable parsed object? In Node.js

Long story short, I used a DataForm object that contains a blob of an image as one of the parts and sent it to my node.js server into the formidable parser:
//blob enters the files variable
formParser.parse(req, (err, fields, files)=>{
if(err)throw err;
const fileInfo = JSON.parse(fields.JSON);
//figure out how to read the blob, how to get it's array buffer, then write to C disk
console.log(files);
//fs.writeFile() here
})
How do I extract the Blobs ArrayBuffer from this persistent file in formidable that looks like this:
{
Binary: PersistentFile {
_events: [Object: null prototype] { error: [Function (anonymous)] },
_eventsCount: 1,
_maxListeners: undefined,
lastModifiedDate: 2022-06-15T17:44:50.214Z,
filepath: 'C:\\Users\\rnata\\AppData\\Local\\Temp\\f962bfe8ab505addfba1e4600',
newFilename: 'f962bfe8ab505addfba1e4600',
originalFilename: 'blob',
mimetype: 'image/png, image/jpg, image/jpeg',
hashAlgorithm: false,
size: 373021,
_writeStream: WriteStream {
_writableState: [WritableState],
_events: [Object: null prototype],
_eventsCount: 1,
_maxListeners: undefined,
path: 'C:\\Users\\rnata\\AppData\\Local\\Temp\\f962bfe8ab505addfba1e4600',
fd: 4,
flags: 'w',
mode: 438,
start: undefined,
autoClose: true,
pos: undefined,
bytesWritten: 373021,
closed: false,
[Symbol(kFs)]: [Object],
[Symbol(kCapture)]: false,
[Symbol(kIsPerformingIO)]: false
},
hash: null,
[Symbol(kCapture)]: false
}
}
What classes do I need, or what methods do I have to use, so that I can extract an arraybuffer and use it in fs.writeFile().
Thank you!
I was having the same issue. In my case I was be able to fix it in that way:
Get filepath in that PersistentFile object.
import * as fs from "fs";
const srcToFile = (src: string) => fs.readFileSync(src);
Send that filepath to srcToFile function. It is going to return to you a buffer/Object.
I was having hard time to upload my formdata's file to AWS S3. After parsing the multiform data and converting the files to Buffer, I got be able to upload it. I hope it works for you too.

Node.js - create dynamic json object and write to .json file

I am new to JS ecosystem and a requirement is to create a json object dynamically at run time and to write it to .
{
"prerequisite": [
{
"url1": "",
"step1": {},
"step2": {}
}
],
"PF": [
{},
{}
]
}
I tried using new Object() but not sure how to build this complex one (to my extent its complex ).
For writing it to file i understand using FS we can achieve it.
Need support on building this json object at run time
I guess you'd like to do something like this. I'm not sure how you want to make it dynamic, but if you have a json object, you can always add to it like this: json_obj.something = "anything"
And it save (overwrite) every time when it runs. Save this code to a file (like filename.js) and run it. (node filename.js)
Oh and must be stringify the json since the FS "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView
const fs = require("fs")
const FILE = "./json_obj.json"
const json_obj = {
prerequisite: [
{
url1: "",
step1: {},
step2: {},
},
],
PF: [{}, {}],
}
fs.writeFile(FILE, JSON.stringify(json_obj), (err) => {
if (err) {
console.error(err)
return
}
})

NodeJS - How to get response of URL?

I have an url adress. When I go that adress in browser it shows a json file. I need to get that json file in NodeJs.
How can I do that?
Edit: This is my current code:
const got = require('got');
var url = "https://www.instagram.com/p/CFcyO54Hc7k/?__a=1";
got(url).json().then(result => {
console.log(result);
}).catch(err => {
console.log(err);
});
And This is the result I get:
Promise { <pending> }
Hint: hit control+c anytime to enter REPL.
RequestError: Unexpected token < in JSON at position 0 in "https://www.instagram.com/accounts/login/?next=/p/CFcyO54Hc7k/%3F__a%3D1"
at Object.parseBody [as default] (/home/runner/VengefulUltimateSdk/node_modules/got/dist/source/as-promise/parse-body.js:22:15)
at /home/runner/VengefulUltimateSdk/node_modules/got/dist/source/as-promise/index.js:157:40
at JSON.parse (<anonymous>)
at parseJson (/home/runner/VengefulUltimateSdk/node_modules/got/dist/source/index.js:118:35)
at Object.parseBody [as default] (/home/runner/VengefulUltimateSdk/node_modules/got/dist/source/as-promise/parse-body.js:11:48)
at /home/runner/VengefulUltimateSdk/node_modules/got/dist/source/as-promise/index.js:157:40
at processTicksAndRejections (internal/process/task_queues.js:97:5) {
name: 'ParseError',
code: undefined,
timings: {
start: 1601274389096,
socket: 1601274389096,
lookup: 1601274389106,
connect: 1601274389117,
secureConnect: 1601274389128,
upload: 1601274389128,
response: 1601274389467,
end: 1601274389511,
error: undefined,
abort: undefined,
phases: {
wait: 0,
dns: 10,
tcp: 11,
tls: 11,
request: 0,
firstByte: 339,
download: 44,
total: 415
}
}
}
My node version is 12.16.1
If you want to get it as json you can use the fetch api (https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) then in the promises you can read the json as an object in your js code.
fetch('http://server/file.json').then(resp=>resp.json()).then(data=>{do something with data});
You can use axios in NodeJS.
axios.get('https://someapiendpoint.domain.com/v1/api')
.then((response) => {
console.log(response.data);
});
You can either parse the JSON yourself or you can get a library for making an Http request that parses the JSON automatically for you. There are lots of libraries to choose from here. My favorite is got() and it will let you parse the JSON with a simple .json() added to it so the resolved value you get back is a Javascript object.
const got = require('got');
got("https://www.instagram.com/p/CFcyO54Hc7k/?__a=1").json().then(result => {
console.log(result);
}).catch(err => {
console.log(err);
});
When I run the above code with your instagram URL, I get this Javascript object back:
{
graphql: {
shortcode_media: {
__typename: 'GraphSidecar',
id: '2404017227651993316',
shortcode: 'CFcyO54Hc7k',
dimensions: [Object],
gating_info: null,
fact_check_overall_rating: null,
fact_check_information: null,
sensitivity_friction_info: null,
media_overlay_info: null,
media_preview: null,
display_url: 'https://scontent-sjc3-1.cdninstagram.com/v/t51.2885-15/e35/119980944_890957481429986_8757958615208227911_n.jpg?_nc_ht=scontent-sjc3-1.cdninstagram.com&_nc_cat=102&_nc_ohc=w7zBW_FdRNgAX9BYknl&_nc_tp=18&oh=11424efc118b55180e4d3302d75181ca&oe=5F99EB23',
display_resources: [Array],
is_video: false,
tracking_token: 'eyJ2ZXJzaW9uIjo1LCJwYXlsb2FkIjp7ImlzX2FuYWx5dGljc190cmFja2VkIjp0cnVlLCJ1dWlkIjoiZGIyMzdkMzgzYWVhNDY5YmI0ZWVkZWI4YzlhZDY0YWUyNDA0MDE3MjI3NjUxOTkzMzE2In0sInNpZ25hdHVyZSI6IiJ9',
edge_media_to_tagged_user: [Object],
edge_media_to_caption: [Object],
caption_is_edited: false,
has_ranked_comments: false,
edge_media_to_parent_comment: [Object],
edge_media_to_hoisted_comment: [Object],
edge_media_preview_comment: [Object],
comments_disabled: false,
commenting_disabled_for_viewer: false,
taken_at_timestamp: 1600801207,
edge_media_preview_like: [Object],
edge_media_to_sponsor_user: [Object],
location: null,
viewer_has_liked: false,
viewer_has_saved: false,
viewer_has_saved_to_collection: false,
viewer_in_photo_of_you: false,
viewer_can_reshare: true,
owner: [Object],
is_ad: false,
edge_web_media_to_related_media: [Object],
edge_sidecar_to_children: [Object],
edge_related_profiles: [Object]
}
}
}
If you'd rather modify your existing code, then you will probably need to use JSON.parse(). You will have to add your existing code to your question for us to help with that.

how to access json data contained in a GET response

Continuing yesterday's saga, now I can retrieve json objects in a response but I can't extract the data from them.
The following node.js snippet is from the file "accounts.js" which is in an ETrade api library that exists in the path /lib. It returns json containing data about the accounts of the authenticated user. The authentication part is working great.
exports.listAccounts = function(successCallback,errorCallback)
{
var actionDescriptor = {
method : "GET",
module : "accounts",
action : "accountlist",
useJSON: true,
};
this._run(actionDescriptor,{},successCallback,errorCallback);
};
The ETrade website says this call will produce the following sample response:
{
"AccountListResponse": {
"Account": [
{
"accountDesc": "MyAccount-1",
"accountId": "83405188",
"marginLevel": "MARGIN",
"netAccountValue": "9999871.82",
"registrationType": "INDIVIDUAL"
},
{
"accountDesc": "MyAccount-3",
"accountId": "83405553",
"marginLevel": "CASH",
"netAccountValue": "100105468.99",
"registrationType": "INDIVIDUAL"
},
{
"accountDesc": "SIMPLE IRA",
"accountId": "83405188",
"marginLevel": "CASH",
"netAccountValue": "99794.13",
"registrationType": "IRA"
}
]
}
}
In my app.js file, I have the following:
var etrade = require('./lib/etrade');
var et = new etrade(configuration);
et.listAccounts(
function(res){
var listAccountsRes = res;
console.log('account list success!');
console.log(listAccountsRes)
},
function(error) {
console.log("Error encountered while attempting " +
"to retrieve account list: " +
error);
});
When I run this code, the console log shows the following message:
{ 'json.accountListResponse':
{ response:
[ [Object],
[ [Object],
[ [Object],
[ [Object],
[ [Object],
[ [Object],
[ [Object],
[ [Object] ] } }
Suppose in app.js I want to put the accounts data in a variable called myAccounts.
One of our members, Jack, solved yesterday's problem and when I commented that I still couldn't access the data in the response, he suggested this: "That property has a dot in it so you'll have to use [ ... ] rather than dot notation to access it. See what's inside the objects with a['json.accountListResponse'].response." So far I have not been able to get that to work, even when I use ['json.accountListResponse'].res like this:
var listAccountsRes = [json.accountListResponse].res;
This returns undefined when printed to the console.
Thanks to Adam for his suggestion which led to this which works:
var listAccountsRes = res['json.accountListResponse'];
var listAccounts = listAccountsRes['response'];
console.log('account list success!');
console.log(listAccounts)
Now the console log reports almost exactly what ETrade says I should get. (They appear to have changed the name "Account" to "response"). I presume my variable listAccounts now contains the json with eight sample accounts in it that I can see in my console log. But I still don't know how to access individual elements. There should be some simple code that will iterate over the json file and produce an array of arrays that I could actually use for something. I tried accessing it like an array: console.log(listAccounts[0]) but that returns undefined. Do I need to stringify it or something?

How to retrieve an [object Object] value in a Twitter API response

I'm currently using a twitter API (twit npm package) and I am currently getting a response with the structure like so
{ metadata: [Object],
I20150213-22:24:29.155(0)? created_at: 'Fri Feb 13 22:24:07 +0000 2015',
I20150213-22:24:29.156(0)? id: 566362206468845600,
I20150213-22:24:29.156(0)? id_str: '566362206468845569',
I20150213-22:24:29.157(0)? text: 'Photo: New BTS picture from Mockingjay part http://t.co/fo30mcLRYV',
I20150213-22:24:29.158(0)? source: 'Tumblr',
I20150213-22:24:29.158(0)? truncated: false,
I20150213-22:24:29.159(0)? in_reply_to_status_id: null,
I20150213-22:24:29.159(0)? in_reply_to_status_id_str: null,
I20150213-22:24:29.160(0)? in_reply_to_user_id: null,
I20150213-22:24:29.160(0)? in_reply_to_user_id_str: null,
I20150213-22:24:29.161(0)? in_reply_to_screen_name: null,
I20150213-22:24:29.161(0)? user: [Object],
I20150213-22:24:29.162(0)? geo: null,
I20150213-22:24:29.162(0)? coordinates: null,
I20150213-22:24:29.163(0)? place: null,
I20150213-22:24:29.163(0)? contributors: null,
I20150213-22:24:29.164(0)? retweet_count: 0,
I20150213-22:24:29.288(0)? favorite_count: 0,
I20150213-22:24:29.289(0)? entities: [Object],
I20150213-22:24:29.289(0)? favorited: false,
I20150213-22:24:29.290(0)? retweeted: false,
I20150213-22:24:29.290(0)? possibly_sensitive: false,
I20150213-22:24:29.291(0)? lang: 'en' },
I am trying to retrieve the entities object to access the potential media items inside it however when I console.log() the entities key in this Array [object Object] gets logged. How do I access the inside of the object? I intend to extract a picture url from inside this object but I can't see whats inside. According to the Twitter API website media is stored inside entities objects.
Any help would be appreciated.
The URL's to the image is stored inside the entity media in the JSON response the REST call returns.
Say you want to access the media_url entity, simply access it as below
var result = the result from your call...
var imageUrl = result.entities.media.media_url;
and use the media URL in whatever way you want.
If you want to check the existence of media content, simply use if(result.entities.media.length > 0).

Categories