Await Buffer.from(String) turns out to numbers (Javascript) - arrays

i am trying desperately to solve the following problem. I researched a lot already but nothing really solved the problem (the used language is Javascript with Node.js and react-bootstrap library).
I want to write the following Array to Buffer and save it to IPFS in a way, that i can read it out later.
Nevertheless, the IPFS.Add() Method demand a buffer object, so i struggle to create the buffer object.
Here the array:
const line_supplier = new Array({
Lieferant: "Yello" ,
Postleitzahl: "13752" ,
Arbeitspreis: "5" ,
Grundpreis: "10" ,
Email: "email"
});
Sounds pretty easy, i know. If i do it like this, the console shows me a an empty buffer.
const line_buffer = await Buffer.from(line_supplier);
console.log(line_buffer);
I also tried ..line_supplier[0], or the recommendations with ..'UTF-8' as offset. I also declared the array directly in Buffer.from(declaration).
Either way, i get an error or just a couple of numbers out. Maybe this is already the mistake. I expect the buffer to be readable like a string?
So i converted the line supplier to a string with
const line_string = JSON.stringify(line_supplier);
But even with this string injected in Buffer.from(), i only get a Uint8Array(84) Object out with a lot of numbers.
I dont know what i am missing. It must be something very small. I read all the description of the methods already, but cant find it.
Thanks in advance!!!

The solution is, the following:
const line_string = JSON.stringify(line_supplier);
console.log(line_string);
const line_buffer = await Buffer.from(line_string);
console.log(line_buffer);
await ipfs.add(line_buffer, (err, ipfsHash) => {
console.log(err,ipfsHash);
Hence you get an array saved the way you want!

Related

Ambiguous use of 'init' in Array mapped to String with chosen separators

As #vadian suggested, I am learning Xcode from Paul Hudson's 100 days of SwiftUI to better understand the basics (even though I am still struggling with time to deliver working GitHub repository search - no it is not for the job I am working on right now).
I am doing a lesson on Arrays, so pretty basic stuff right now and I struggle to map the String from array on the sorted array. I don't get why, but I get the:
Ambiguous use of 'init'
error.
Here's the code:
let cities = ["London", "Tokyo", "Rome", "Budapest"]
print(cities.sorted())
let citiesSorted = cities.sorted()
let citiesSortedString = citiesSorted.map(String.init).joined(separator:", ")
And it's so really strange, since I pulled the similar thing out before in the same Playground but not with the sorted Array:
var schoolScoresString = schoolScores.map(String.init).joined(separator:", ")
print(schoolScoresString)
And yes I tried changing let to var. It didn't help.
cities is already of type [String], so you're passing a String to String.init, which obviously won't work. If you want to join an array of Strings into a single String, remove the map and just call joined on the array.
let citiesSortedString = citiesSorted.joined(separator:", ")

Externally save specific value (one HUGE array) of a Huge JSON file - Node

So I have a HUGE JSON external file that I want to read in my nodejs project and save the value of a specific key from that JSON file to another external JSON file, where the value itself is one HUGE array.
The general structure of the input JSON :
{
key1: val1,
key2: val2,
key3: [val3_1, val3_2, ...],
key4: {
key4_1: val4_1,
key4_2: [val4_2_1, val4_2_2, ...]
}
...
}
I am not sure if reading line by line, as what I have read almost everywhere to read HUGE JSON, a way to proceed as I want to perform a search operation in a way.
So, thanks to #T.J.Crowder, I managed to locate a wrong method call I was making. Got a working code now:
const StreamObject = require('stream-json/streamers/StreamObject');
const fs = require('fs');
const _ = require('underscore');
const jsonStream = StreamObject.withParser();
var inputfile = "~Path/5cd792a633e32a6e5e20e56a.geojson";
var outputfile = "~Path/5cd792a633e32a6e5e20e56a.json";
var outstream = fs.createWriteStream(outputfile);
outstream.writable = true;
jsonStream.on('data', ({key, value}) => {
if (_.difference(['features'], Object.keys(value)).length === 0 ){
outstream.write(JSON.stringify(Object.values(value['features'])));
}
});
jsonStream.on('end', () => console.log('Done Export!'));
fs.createReadStream(inputfile).pipe(jsonStream.input);
Basically, all I am doing is reading a geojson file that is stored locally and exporting the array of data['data']['features'] to another external json file. This is just an sample here and geojson and exported array/json might get pretty big.
NOW, although I managed to achieve it by merging different stackoverflow posts, not sure if it's supposed to do the right job and the whole array is not being stored in RAM at once. Especially, the way if statement is being used to write output. Please correct the code, if necessary.
Thanks!

Access $firebaseObject value with variable instead of string

I need to assign a variable to a string and pass that variable to a Firebase query in a $firebaseObject. But, when I try this, the firebaseObject is null. I tried with directly putting the string in and it works, but I don't want that.
Code:
//var itemkey='-JyO7Zsgsucf5ESttJwt';
var itemkey=window.localStorage['fbid'];-------> string: '-JyO7Zsgsucf5ESttJwt'
console.log(typeof itemkey); ------------------> string
$scope.galphoto = $firebaseObject(ref.child(itemkey));
console.log($scope.galphoto) ------------------> null
When I call
$scope.galphoto = $firebaseObject(ref.child(itemkey));
With itemkey='-JyO7Zsgsucf5ESttJwt',
then console.log($scope.galphoto) is properly shown. However, when I use what I really want to use,
window.localStorage['fbid']
then console.log($scope.galphoto) is null.
Why?
I think you may be trying to console.log the result before it's available. $firebaseObject returns the data with some delay. You may try to do something like
$scope.galphoto = $firebaseObject(ref.child(itemkey));
$scope.galphoto.$loaded().then(function () {
console.log($scope.galphoto);
}
You should see the expected result, since you're waiting for the promise to respond. It could help you better understand how to get the desired result.

Unable to translate bytes [...] at index ... from specified code page to Unicode when adding to index

I am using Newtonsoft.Json to create the JSON to update add items to an index, but I get the following error when I POST the request:
{"error":{"code":"","message":"The request is invalid.","innererror":{"message":"parameters : Unable to translate bytes [E3] at index 752 from specified code page to Unicode.\r\n","type":"","stacktrace":""}}}
I know the error occurs with some non letter characters in some of the strings in the data that I am serializing. The string data comes from SQL, so I'm guessing something is going on to do with encoding that I cannot figure out.
When I inspect the JSON string, and put it in manually construct a request with the same data in Fiddler it all works fine.
Does anyone have any idea what might be the problem, and how I can work around it?
I found my own solution after a bit more digging.
Adding "StringEscapeHandling.EscapeNonAscii" to the serialization options solves the problem:
jsonSettings = new JsonSerializerSettings
{
Formatting = Newtonsoft.Json.Formatting.Indented,
ContractResolver = new CamelCasePropertyNamesContractResolver(),
DateTimeZoneHandling = DateTimeZoneHandling.Utc,
StringEscapeHandling = StringEscapeHandling.EscapeNonAscii
};

how do i extract a value from a lua array?

I'm debugging someone else's application and I've run across a data structure that, when dumped to a file, looks like this:
["value"] = {}
["value"]["0.ouname"] = {}
["value"]["0.ouname"]["label"] = "Test value"
["value"]["0.ouname"]["seq"] = 90
["value"]["0.ouname"]["type"] = "text"
["value"]["0.ouname"]["value"] = ""
["value"]["1.ouname"] = {}
["value"]["localityName"]["value"] = "California"
I need to be able to extract the seq number "90" from it but I'm at a loss as to how.
I am able to get at the "California" value by doing the following:
print(myvar.value.localityName.value)
However, I can't seem to get the sequence number
So far, I've tried the following:
print(myvar.value.0.ouname.seq)
print(myvar.value.["0.ouname"].seq)
print(myvar.value."0.ouname".seq)
But I haven't been successful!
If you have any suggestions, I'd appreciate it.
In Lua, a.b is syntactic sugar for a["b"]. Of course, you can use it only when it's possible, and the resulting expression won't mean something else (as in your example).
Thus, you have to use
print(myvar.value["0.ouname"].seq)
You could also drop all dots and use only [], as such:
print(myvar["value"]["0.ouname"]["seq"])
Which is exactly the same format that has been written to file.
print(myvar.value['0.ouname'].seq)

Resources