#data should be a string, Buffer or Uint8Array

14 messages · Page 1 of 1 (latest)

warped matrix
#

Getting this error while using form-data when making Post call

#

kindly help me on this to resolve it

keen spruce
#

can you show us the whole error and the request you've made?

copper gorge
#

Like the error says, you need to append data either as a string, a buffer, or a Unit8Array

warped matrix
#

function where I am forming the form data async sendEmailWithAttachment(data:any, templateId:string,fileData:any) { if (!this.configService.get<boolean>('email.enabled')) { return null; } try { const formData = new FormData(); const fileOptions = { knownLength: statSync(fileData.filepath).size, filename: fileData.filename, }; formData.append( 'attachmentFiles', createReadStream(fileData.filepath), fileOptions, ); formData.append(`data["name"]`, data.name); formData.append(`data["from_date"]`, data.from_date); formData.append(`data["to_date"]`, data.to_date); formData.append("from",this.configService.get<string>('email.fromEmail')); formData.append("to", fileData.sendToEmail); formData.append("templateId", templateId); formData.append("requestId", fileData.requestId); formData.append("isExternalStorage","false")

#
         
            const headers = {
                ...formData.getHeaders(),
                'x-api-key': this.configService.get('xApiKey'),
                'Content-Length': formData.getLengthSync().toString(),
                'x-correlation-id':fileData.correlationId || uuid()
            }
            this.logger.log(`Generating email with payload: ${JSON.stringify(formData)}`);
            const response = await this.requestHelper.postCallTransactions(url, formData, headers, {});
            this.logger.log(`Equitas email generation response: ${JSON.stringify(response)}`)
            return response;
        } catch (err3) {
            this.logger.error(`Equitas generate email api failed with error: ${JSON.stringify(err3)}`)
            throw err3
        }
        
        
      }  ```
copper gorge
#

What property is showing the error?

warped matrix
#

====== error======= TypeError: data should be a string, Buffer or Uint8Array
at RedirectableRequest.write (/home/node_modules/follow-redirects/index.js:84:11)
at FormData.ondata (node:internal/streams/legacy:20:31)
at FormData.emit (node:events:329:20)
at FormData.EventEmitter.emit (node:domain:467:12)
at FormData.CombinedStream.write (/home/node_modules/combined-stream/lib/combined_stream.js:138:8)
at FormData.CombinedStream._pipeNext (/home/node_modules/combined-stream/lib/combined_stream.js:126:8)
at FormData.CombinedStream._realGetNext (/home/node_modules/combined-stream/lib/combined_stream.js:99:10)
at FormData.CombinedStream._getNext (/home/node_modules/combined-stream/lib/combined_stream.js:82:12)
at FormData.CombinedStream.resume (/home/node_modules/combined-stream/lib/combined_stream.js:154:10)
at FormData.CombinedStream.pipe (/home/node_modules/combined-stream/lib/combined_stream.js:66:8)
at dispatchHttpRequest (/home/node_modules/axios/lib/adapters/http.js:419:10)
at new Promise (<anonymous>)
at httpAdapter (/home/node_modules/axios/lib/adapters/http.js:49:10)
at dispatchRequest (/home/node_modules/axios/lib/core/dispatchRequest.js:58:10)
at Axios.request (/home/node_modules/axios/lib/core/Axios.js:109:15)
at Axios.httpMethod [as post] (/home/node_modules/axios/lib/core/Axios.js:144:19)
at wrap (/home/node_modules/axios/lib/helpers/bind.js:9:15)
at Observable._subscribe (/home/node_modules/@nestjs/common/http/http.service.js:51:13)

keen spruce
#

so it's just a runtime error, right?

warped matrix
#

yes

copper gorge
#

Seems like whatever you think you're sending, you aren't

warped matrix
#

but same code works in dev enviroment but not in production

copper gorge
#

So there's a difference in the code being ran

warped matrix
#

only the url change in dev we use https and in production use http that is private