TypeError:无法读取空NodeJ的属性“名称”

时间:2019-12-30 08:21:50

标签: node.js multipartform-data form-data request-promise

我正在学习NodeJs并在进行实验。我正在使用request-promiseNodeJs调用另一个api。我正在使用form-data创建表单并将其发送到另一个api。 我的片段:

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);
}

我的错误:

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)

请帮助我解决此问题。

2 个答案:

答案 0 :(得分:0)

request-promise在内部处理form-data。因此,无需显式使用form-data。相反,我通过以下方式制作普通对象:

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

答案 1 :(得分:0)

之所以会出现此问题,是因为在requestPromise方法的formData键中,您实际上不需要传递formData而是一个对象。

请求承诺从传递的对象内部创建formData。

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
};