Friday, February 23, 2024
 Popular · Latest · Hot · Upcoming
108
rated 0 times [  109] [ 1]  / answers: 1 / hits: 8393  / 4 Years ago, mon, december 30, 2019, 12:00:00

I am learning and experimenting on NodeJs. I am using request-promise to call another api from NodeJs. I am using form-data to create a form and send it to another api.
My snippet:



const requestPromise = require('request-promise');
const FormData = require('form-data');
....

var sendToAPI = async (fileObjBuffer, myId, timestamp) => {

let formData = new FormData();
formData.append('fileData', fileObjBuffer);
formData.append('myId', myId);
formData.append('fileName', timestamp);

let options = {
method: 'POST',
uri: '<URL>',
formData: formData,
headers: {
'Access-Control-Allow-Origin': '*',
'enctype': 'multipart/form-data',
'Content-Type': 'application/json',
'Access-Control-Allow-Methods': 'GET,POST,OPTIONS,DELETE,PUT'
},
json: true
};
try {
let apiResult = await requestPromise(options).promise();
console.log('nnapiResult: ', apiResult);
} catch (error) {
console.log('error in sending to api: ',error);
}
}

var fetchAllData = async () => {
let query = 'select * from demo_db.demo_table;';
let fileObject = ;
var result;
try {
//cassandra query
result = await client.execute(query, [], { prepare: true });
} catch (error) {
console.log('error in fetching data from Cassandra: ',error);
}
result.rows.forEach(resultObj => {
fileObject = fileObject +resultObj['room_id'] +:+resultObj['message_id']+:+resultObj['original_message'] +:+resultObj['send_date'] +:+ resultObj['sender'] +%;
});
let fileObjBuffer = new Buffer(fileObject);
let myId = uuidv4();
let timestamp = date.format(new Date(), 'YYYYMMMDDhhmmss', false);
sendToAPI(fileObjBuffer,myId,timestamp);
}


My error:



error in sending to api:  TypeError: Cannot read property 'name' of null
at FormData._getContentDisposition (/home/bhushan/NodeJS-Scheduler/node_modules/request/node_modules/form-data/lib/form_data.js:226:40)
at FormData._multiPartHeader (/home/bhushan/NodeJS-Scheduler/node_modules/request/node_modules/form-data/lib/form_data.js:177:33)
at FormData.append (/home/bhushan/NodeJS-Scheduler/node_modules/request/node_modules/form-data/lib/form_data.js:70:21)
at appendFormValue (/home/bhushan/NodeJS-Scheduler/node_modules/request/request.js:326:21)
at Request.init (/home/bhushan/NodeJS-Scheduler/node_modules/request/request.js:337:11)
at Request.RP$initInterceptor [as init] (/home/bhushan/NodeJS-Scheduler/node_modules/request-promise-core/configure/request2.js:45:29)
at new Request (/home/bhushan/NodeJS-Scheduler/node_modules/request/request.js:127:8)
at request (/home/bhushan/NodeJS-Scheduler/node_modules/request/index.js:53:10)
at sendToAPI (/home/bhushan/NodeJS-Scheduler/schedulerTest.js:52:25)
at fetchAllData (/home/bhushan/NodeJS-Scheduler/schedulerTest.js:95:2)
at process._tickCallback (internal/process/next_tick.js:68:7)


Please help me to solve this issue.


More From » node.js

 Answers
5

request-promise internally handles form-data. Therefore no need to use form-data explicitly. Instead I made normal object in following way:



var formData = {
fileData: {
value: fileObjBuffer,
filename: timestamp
},
fileName: timestamp,
myId: myId
}

[#5208] Wednesday, December 25, 2019, 4 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
marvinm

Total Points: 406
Total Questions: 104
Total Answers: 121

Location: Iceland
Member since Tue, Jan 25, 2022
2 Years ago
marvinm questions
Sun, Dec 19, 21, 00:00, 2 Years ago
Thu, Apr 16, 20, 00:00, 4 Years ago
;