The “size” argument must be of type number in Nodejs
The “size” argument must be of type number in Nodejs
I want to onverting mongodb Binary _id to UUID using node so in order a googling and find my response at this post :
I want to insert this UUId into postgres database.This is my pg function on node.js:
function insert(arg)
console.log('insert location');
arg.forEach(element =>
(async () =>
await pool.query(
`insert into public."Location" ("Id", "Lat", "Lng", "CreationDateTime", "DeviceId", "Topic", "UserId")
VALUES ('$uuidv1()', $element.Lat, $element.Lng, $1, $element.DeviceId, $2, $3)`,[element.CreationDateTime, element.Topic,Bin2HexUUID(element.UserId)]);
)();
);
This is element.UserId
:
element.UserId
Binary _bsontype: "Binary", sub_type: 4, position: 16, …
_bsontype:"Binary"
and this is Bin2HexUUID
method:
Bin2HexUUID
function Bin2HexUUID(bin)
var hex = new Buffer.alloc(bin, 'base64').toString('hex');
return hex.replace(/^(.8)(.4)(.4)(.4)(.12)$/, function ()
return arguments[1]+"-"+arguments[2]+"-"+arguments[3]+"-"+arguments[4]+"-"+arguments[5];
);
When i run script i got this error:
new Buffer.alloc(bin, 'base64').toString('hex')
NodeError: The "size" argument must be of type number. Received type object
----------------------
(node:2292) UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_ARG_TYPE]: The "size" argument must be of type number. Received type object
For first time i used new Buffer(bin, 'base64').toString('hex')
but seems new Buffer is deprecated..
new Buffer(bin, 'base64').toString('hex')
This is my Node and Npm version:
node -v
v10.8.0
npm -v
6.2.0
2 Answers
2
The size
argument inBuffer.alloc(size)
, not unreasonably, requires a number, because it's an argument that tells the method how many bytes to allocate.
size
Buffer.alloc(size)
Your Bin2HexUUID(element.UserId)
is passing the element.UserId
object itself to this method (your error message is telling you that when it says "Received type object"). That's not what the method wants. What it wants is the size (length) of Element.UserId
.
Bin2HexUUID(element.UserId)
element.UserId
Element.UserId
So, get the length of Element.UserId
and pass that instead.
Element.UserId
I changed this part
Bin2HexUUID(element.UserId)
to this:
Bin2HexUUID(element.UserId.buffer)]);
and method :
function Bin2HexUUID(bin)
var hex = new Buffer.from(bin, 'base64').toString('hex');
return hex.replace(/^(.8)(.4)(.4)(.4)(.12)$/, function ()
return arguments[1]+"-"+arguments[2]+"-"+arguments[3]+"-"+arguments[4]+"-"+arguments[5];
);
I hope this changes help to guys.
Yes dude. I got
.buffer
from my UserId binary object and pass to those method and those method return a UUId as a return value @BobRodes– sayres kabir
Aug 26 at 4:38
.buffer
Glad you got it solved. :) You might come back after 48 hours and accept your own answer.
– BobRodes
Aug 26 at 4:53
Sure dude. Thank you @BobRodes
– sayres kabir
Aug 26 at 4:53
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Are you saying that this solved your problem?
– BobRodes
Aug 26 at 4:35