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('\n\napiResult: ', 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.
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('\n\napiResult: ', 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.
Share Improve this question edited Apr 5, 2023 at 23:26 benomatis 5,6437 gold badges39 silver badges60 bronze badges asked Dec 30, 2019 at 8:21 Bhushan MahajanBhushan Mahajan 1713 gold badges3 silver badges14 bronze badges 9-
The code what you have pasted doesn't contain the error part. The error states that
name of null
means you are trying to access name ofnull
which may be an object or array, so paste your full code otherwise its difficult – Subburaj Commented Dec 30, 2019 at 8:25 - See stackoverflow./questions/44597174/… – user9572013 Commented Dec 30, 2019 at 8:29
-
@Subburaj, I have just written another simple function
fetchAllData
to createfileName
,myId
, andfileObjBuffer
values and then calledsendToAPI()
fromfetchAllData
. – Bhushan Mahajan Commented Dec 30, 2019 at 8:34 - Hi, @VictorF, I have seen the post but I didn't understand, how to implement it in my case. – Bhushan Mahajan Commented Dec 30, 2019 at 8:38
- Is this all of your code? – user9572013 Commented Dec 30, 2019 at 8:39
3 Answers
Reset to default 2request-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
}
This issue arises because in the formData key of requestPromise method you do not need to actually pass formData but an object.
request-promise internally creates formData from the object passed.
let formData = {
fileData: fileObjBuffer,
fileName: timestamp,
myId: myId
}
let options = {
method: 'POST',
uri: '<URL>',
formData: formData, // formData here is an object
headers: {
'Access-Control-Allow-Origin': '*',
'enctype': 'multipart/form-data',
'Content-Type': 'application/json',
'Access-Control-Allow-Methods': 'GET,POST,OPTIONS,DELETE,PUT'
},
json: true
};
I did not find any good solution. So, I have added a function which will test if the value is empty or not. if it is empty then don't add it into the formData object else add it.
const FormData = z.require('form-data');
const form = new FormData();
const addFormData = (key, value) => {
// If value is an array
if(Array.isArray(value) && value.length > 0) {
form.append(key, JSON.stringify(value));
return;
}
// If value is string or number
if(value) {
form.append(key, value);
}
// TODO: You can add aditional checks if you would like to verify if it is non-empty object etc
}
addFormData('name', 'Rahul')
addFormData('age', 28)
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744215336a4563528.html
评论列表(0条)