HI everybody,
I have this code, i dont know what is the error, please help me!!!!
module.exports.add_defect_attachment = function(toolinfo, defectid, filename, filedata, callback)
{
var attachmentdata, tempDirPath, filePath;
var onFileWritten = function(err) {
if (err) {
var res = {};
res.statusCode = 504;
res.statusMessage = 'Cannot write file in temp directory';
callback(res, {});
}
else {
var req = toolinfo.req_header + '/issue/' + defectid + '/attachments';
console.log('req '+req);
toolinfo.cnx.request('POST', req, null, { 'file' :'@'+filePath }, function(res, jsonData) {
fs.unlinkSync(filePath);
callback(res, jsonData);
});
}
}
if (typeof filedata == 'string') {
attachmentdata = new Buffer(filedata, 'binary');
} else{
attachmentdata = filedata;
}
tempDirPath = path.join(__dirname,'..', 'temp');
if (!fs.existsSync(tempDirPath)) {
fs.mkdirSync(tempDirPath);
}
filePath = path.join(tempDirPath, filename);
/* var data =JSON.stringify( {
name: 'fom',
filename : filePath
});
*/
console.log('dataaa '+data);
if (!fs.existsSync(filePath)) {
fs.writeFile(filePath, attachmentdata, "binary", onFileWritten);
} else
{
onFileWritten();
}
}
I always get this error
error: uncaughtException: Unexpected token < in JSON at position 0