2

I am getting a buffer data from frontend to nodejs as below

 <Buffer 53 6f 75 72 63 65 53 63 68 65 6d 61 2c 43 6f 75 6e 74 72 79 4e 61 6d 65 2c 49 44 2c 41 4d 54 5f 45 58 43 4c 5f 54 41 58 2c 41 4d 54 5f 49 4e 43 4c 5f ... 340629006 more bytes>

I am trying to save this data to database using sequelize as below

const DBMODEL = require("../../../models/SaveBufferData")

module.exports = async (req, res) => {

try{
var bufdata = req.file.buffer;
var response = await DBMODEL.SaveBufferData(req.body.fileId, bufdata);

} catch (error) {
   console.log('errorerror',error)
   res.status(500).json(ResponseManager(false, error.message));
 }

I am getting an error as below

node:buffer:669
slice: (buf, start, end) => buf.hexSlice(start, end),
                                ^

Error: Cannot create a string longer than 0x1fffffe8 characters
    at Object.slice (node:buffer:669:37)
    at Buffer.toString (node:buffer:811:14)
    at BLOB._stringify (C:\IWA-BACKEND\PRIMS-local\PRIMS - Iwa\BACKEND\node_modules\sequelize\dist\lib\data-types.js:423:23)
    at BLOB.stringify (C:\IWA-BACKEND\PRIMS-local\PRIMS - Iwa\BACKEND\node_modules\sequelize\dist\lib\data-types.js:22:19)
    at escape (C:\IWA-BACKEND\PRIMS-local\PRIMS - Iwa\BACKEND\node_modules\sequelize\dist\lib\sql-string.js:40:48)
    at C:\IWA-BACKEND\PRIMS-local\PRIMS - Iwa\BACKEND\node_modules\sequelize\dist\lib\sql-string.js:101:14
    at String.replace (<anonymous>)
    at Object.formatNamedParameters (C:\IWA-BACKEND\PRIMS-local\PRIMS - Iwa\BACKEND\node_modules\sequelize\dist\lib\sql-string.js:96:14)
    at Object.formatNamedParameters (C:\IWA-BACKEND\PRIMS-local\PRIMS - Iwa\BACKEND\node_modules\sequelize\dist\lib\utils.js:112:20)
    at Sequelize.query (C:\IWA-BACKEND\PRIMS-local\PRIMS - Iwa\BACKEND\node_modules\sequelize\dist\lib\sequelize.js:283:21) {
  code: 'ERR_STRING_TOO_LONG'
}

Why am I getting this error? can anyone please explain

Sai sri
  • 515
  • 12
  • 25
  • you're getting this error because apparently the string you are trying to make is longer than 536,870,888 characters - you should've searched stack overflow ... https://stackoverflow.com/questions/68230031/cannot-create-a-string-longer-than-0x1fffffe8-characters-in-json-parse https://stackoverflow.com/questions/68493636/cannot-create-a-string-longer-than-0x1fffffe8-characters-in-react-native https://stackoverflow.com/questions/63840115/react-native-failed-to-construct-transformer-error-cannot-create-a-string-lo ... there's MANY questions (and answers) – Bravo Mar 07 '22 at 08:10
  • @Bravo So what is the solution? I have to save it to db for future use – Sai sri Mar 07 '22 at 08:11
  • I believe you should not store such large object in DB. Instead, use some storage e.g. S3 – Waleed Ahmad Mar 07 '22 at 08:12
  • I dont have an s3 option, I have to save it in db – Sai sri Mar 07 '22 at 08:13
  • Perhaps one of the other questions on this topic has an answer. Research – Bravo Mar 07 '22 at 08:13
  • I have done a research I didnt find anything related to db. Can u provide the link please – Sai sri Mar 07 '22 at 08:19
  • @Bravo or do u have any suggestions other than db and s3? – Sai sri Mar 07 '22 at 08:27
  • other than db? you said you HAVE to save in db ... did you search and read all the answers - there isn't really that many on stack overflow, but surely someone else tried to do what you want – Bravo Mar 07 '22 at 08:29
  • @Bravo Yes, I have to save it in DB. in worst case if I cant I have to look for an alternative. I have searched in google for the solution, yes even I didnt find many questions in google. also I didnt find the solution for it, thats the reason I have posted a question here. – Sai sri Mar 07 '22 at 08:36
  • The answer is, don't save the data as a string to the "db" - what other data formats does the "db" accept? Perhaps a BLOB – Bravo Mar 07 '22 at 08:45
  • whatever formats are available for sql server, it can be saved in that format. Also we must be able to convert back to the buffer format at nodejs level in future after fetching. – Sai sri Mar 07 '22 at 08:48
  • then a BLOB is what you want Binary Large OBject - and yes, retrieving a blob results in a buffer – Bravo Mar 07 '22 at 08:53
  • How to do it? How to convert to blob? – Sai sri Mar 07 '22 at 08:55
  • @Bravo Or can we save buffer data to a file? – Sai sri Mar 07 '22 at 09:11
  • have you read any sequelize documentation at all - or searched here for how to use blobs with sequelize? – Bravo Mar 07 '22 at 09:27

0 Answers0