Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

nodejs create buffer from string

// a string
const str = "Hey. this is a string!";

// convert string to Buffer
const buff = Buffer.from(str, "utf-8");

console.log(buff); // <Buffer 48 65 79 2e ... 72 69 6e 67 21>
Comment

node convert buffer to string

// Convert Buffer to String in Node.JS
const someBuffer = Buffer.from("Hey", "utf-8");

someBuffer.toString(); // <= Use the toString() method
Comment

buffer to string

Your code is working. The buffer you have is actually the string "[object Object]".

let b = Buffer.from('[object Object]', 'utf8')
console.log(JSON.stringify(b))
// {"type":"Buffer","data":[91,111,98,106,101,99,116,32,79,98,106,101,99,116,93]}

console.log(b.toString('utf8'))
// [Object object]
The problem you need to figure out is why is a buffer with that string being sent. It seems like the sender of the buffer needs to call stringify or otherwise serialize the object before sending it. Then you can turn it back to a string with toString() and use JSON.parse() on the string.
Comment

PREVIOUS NEXT
Code Example
Javascript :: prevstate in usestate 
Javascript :: disable eslint specific rule 
Javascript :: javascript try 
Javascript :: ajax failure response 
Javascript :: regex to indentify url 
Javascript :: cryptojs decrypt 
Javascript :: how to convert string to lowercase in javascript 
Javascript :: node js send fcm 
Javascript :: how to use hidden in div in angular 
Javascript :: shuffle an array of numbers in javascript 
Javascript :: all inputs under div 
Javascript :: sh: 1: react-scripts: not found 
Javascript :: js convert special characters to html entities 
Javascript :: jquery remove link href 
Javascript :: convert to array str js 
Javascript :: javascript change input value 
Javascript :: js get random element in array 
Javascript :: placeholder js 
Javascript :: jquery find by data attribute 
Javascript :: width and height in js 
Javascript :: calculato 
Javascript :: statusbar height react native 
Javascript :: js find longest word in string function 
Javascript :: login page link shopify 
Javascript :: remove property from javascript object 
Javascript :: javascript return promise 
Javascript :: how to remove duplicate values in array of objects using javascript 
Javascript :: random string from array javascript 
Javascript :: use onchange with react select 
Javascript :: react not getting img by src 
ADD CONTENT
Topic
Content
Source link
Name
5+7 =