I'm new to Winston and I've poorly coded a log system that uses json.
When I tried to open that output file as a json, Js gave me an error, then I went to the file and I saw that it isn't well formed, is there something I'm missing here?
This is my json log generated from Winston:
{
"timestamp":"2019-01-11T17:59:47.847Z",
"response":"",
"level":"info",
"message":"log 1"
}
{
"timestamp":"2019-01-11T17:59:47.854Z",
"response":"",
"level":"info",
"message":"log 2"
}
I understant it has to be comma-separated and with brackets, so what's wrong here? Why is Winston throwing multiple separated entries instead of a single json file?
Related
<script type="module" id="user-code">
try {
someUnknownReference;
} catch (error) {
console.log(error) // correct
console.log(error.toString()); // incorrect
console.log(error.stack.toString().split('\n')); // incorrect - line number and filename is wrong
}
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImluZGV4LmpzIl0sIm5hbWVzIjpbImxrc2xrcyJdLCJtYXBwaW5ncyI6IkFBQUFBLE1BQU0iLCJmaWxlIjoiYnVuZGxlLmpzIiwic291cmNlc0NvbnRlbnQiOlsibGtzbGtzIl19
</script>
I'm building a browser based code editor using Babel. All is working well, however I can't for the life of me extract the line number and file name out of the errors that happen during run time.
The example above is a simplified version of what I'm doing. I'm injecting a string into a script tag e.g scriptTag.innerHTML = bundledCode;
The first console.log prints exactly what I want to parse e.g it has the file name and correct line number which it gets from the sourcemap
ReferenceError: someUnknownReference is not defined
at index.js:1
However, as soon as I try to do anything with the error e.g the second console.log, I lose the line number and file name.
ReferenceError: someUnknownReference is not defined
My guess is maybe it's losing the reference to the sourcemap when we try to parse the object?
Thanks in advance for any help!
I think Chrome console is automatically consuming source maps to provide the correct error msg if you're console logging the error. Not if you're grabbing the actual string. Do you need something like this: https://www.npmjs.com/package/sourcemapped-stacktrace?
I'm trying to get JSON raw response from R Plumber and consume it in Angular, but JavaScript Framework thinks it's a string rather than a JSON format.
"[{\"id\":1,\"type\":\"Diagnostic\"},{\"id\":2,\"type\":\"Impact\"}]"
I saw here and here, but these didn't really help me.
How do I make it so that it's a proper JSON format that JavaScript frameworks can recognize.
#* #apiTitle Diagnostic Report API
#* Send the list of report types
#* #get /reportTypes
#* #serializer unboxedJSON
function(){
reportTypes <- read_csv(file = "ReportTypes.csv")
# list(
# message_echo = paste("The text is:", "text")
# )
}
And in the Angular, this is the error that I receive. Given that this is not Angular SO, I just wanted to show the issue I'm running into:
Cannot find a differ supporting object '[{"id":1,"type":"Diagnostic"},{"id":2,"type":"Impact"}]' of type 'string'. NgFor only supports binding to Iterables such as Arrays.
Not the best thing to do to answer your own question, but I found many questions like this but without a particular solution that worked for me. I felt I should write this as an answer for anyone who'd run into similar situation. Hopefully, it'll save someone hours of painful debugging.
As many of the posts including the ones that I mentioned above correctly assert that plumber automatically serializes the r object. This means, it should be ready for any application requesting the data to consume. But clearly, there were something that I was missing. I had already tried solutions based on these.
Turns out, my express.js server was also parsing the text to json.
router.get('/', function (req, res) {
request.get({ url: 'http://localhost:5762/reportTypes' },
function (error, response, body) {
if (!error && response.statusCode == 200) {
res.json(body); <-- this is where it's serializing again
}
});
});
Credit to all SO community and responders including #MrFlick who discussed about JSON parsing, it gave me a thought perhaps there's somewhere something is serializing the data. So, after digging, it turns out that my express is re-serializing the already serialized data. Simply changing res.json to res.send solved the problem.
Hopefully, this will give some ideas to people who either work on r or javascript and have similar problems, to re-check the codes which is requesting the data again and make sure it's not getting serialized and escaping important characters.
I'm working on a discord bot right now that reads responses off of a JSON file. The basic format is as follows:
"matt":
{
"insults" : ["test 1",
"test 2",
"test 3",
"test 4"
]
},
I'm currently working on a function that allows a user to use the !addInsult command, followed by a string, which will then append onto the existing array.
My desired workflow is as such:
User types in the following: !addInsult test 5. This would then modify the JSON object of insults under matt to the following:
"matt":
{
"insults" : ["test 1",
"test 2",
"test 3",
"test 4",
"test 5"
]
},
Doing this will allow me to let my friends add data to my bot without needing me to manually edit the JSON every time we want something new.
What would the best way of going about this be? I've looked into this thing called push, but I don't really understand how that works.
This is what I have so far. I think I'm going in the right direction, but I'm not sure:
The following is established at the beginning of the script:
// contains the insults
var insults = require('./insults.json');
// get the insults from the json file specific to user
var insultsString = JSON.stringify(insults);
var json = JSON.parse(insultsString);
And here is the function that will be doing appending:
// command that allows users to add to the pool of insults
function addInsultCommand(args, receivedMessage)
{
// create an object that contains the information for the json file
json["bot"].push(["test"]);
receivedMessage.channel.send(json.matt.insults[0]);
}
so there is a misconception here; JS does not write to files.
You're using webpack, which let's you require the .json using a webpack loader, but this will ONLY work when using the dev server. This will not work when distributing your code, because the .json will will be encoded into your output bundle.
.js can not write a file for you ( except locally ), so what you need to do is two fold:
1) Download the .json from the webserver, without using a webpack loader.
2) modify the JSON data in memory
3) upload the JSON data to the web server for it to write the file for you.
In addition to this, I can not follow your example code. you reference receivedMessage.channel.send, but I do not see where this is defined. Is this some kind of discord integration? You may need to re-state your question along with a minimal proof of the issue with reproducible test code.
JSON.stringify will turn a javascript object into a JSON object. JSON.parse will do the opposite(turn a JSON object into a Javascript object). Assuming insults.json is a json object, you do not need to convert it into a string. You can just do JSON.parse(insults) to convert it into a javascript object.
I am not sure what you were intending to do with the args variable in addInsultCommand but I am going to ignore for now and give you some steps to follow below.
1) Turn JSON object(insults) into JS object
2) create a function that takes a JS object and a receivedMessage(the insult to add) and assigns it to the correct place in the JS object.
3) convert the JS object into JSON(using JSON.stringify) and replace the contents of insults.json with the the updated value.
I have a Node.js file that outputs a bunch of test results, easily >1000 lines, to Terminal. The file looks something like this:
launchChromeAndRunLighthouse('https://www.apple.com/', flags).then(results => { console.log(results); });
The console.log() is only there because I couldn't figure out another way to view the results. I need a way to create a file through Node.js, not the command line, that contains all of the CLI output/results.
I thought that fs.appendFile('example.txt', 'append this text to the file', (err) => {}); could be of use, but what I need to "append" to the file is the function's results. When I try that, the file only contains [object Object] instead of the actual results of the tests.
I'm a beginner to Node, any advice is highly appreciated.
You are close, but you need to include the appendFile inside of your other function. This assumes that your 'results' object is of the string type. If not then you need to get the string version of this object.
The lighthouse docs specify the format of the log information that is returned. If you add output: json to the flags object then you can use it lik
launchChromeAndRunLighthouse('https://www.apple.com/', flags).then(results => {
fs.appendFile('example.txt', JSON.stringify(results), (err) => {
console.log('error appending to file example.txt');
});
});
I'm developing a web app with Node.js using Sails framework(based on Express) and i'm using a third party image solution called Transloadit (no need to know Transloadit).
Anyway, that's not the problem, i'm been able to implement the Transloadit form and receive the information from their API.
My problem is that, Transloadit gives me the response as a String, and I need to access the response objects, so i'm using var objRes = JSON.parse(req.body.transloadit); to parse it to an JSON object, and when I console.log(objRes); the object is not correctly parsed, i get this: (see all JSON here https://gist.github.com/kevinblanco/9631085 )
{
a bunch of fields here .....
last_seq: 2,
results: {
thumb: [
[
Object
]
]
}
}
And I need the data from the thumb array, my question is, Why is doing that when parsing ?
Here's the entire request req.body object: https://gist.github.com/kevinblanco/9628156 as you can see the transloadit field is a string, and I need the data from some of their fields.
Thanks in advance.
There is nothing wrong with the parsing of the JSON -- in fact there is no problem at all.
consol.log limits the depth of what it is printing which is why you are seeing [object] in the output.
If you want to see the full output in node.js then just use the inspect utility like this;
console.log(util.inspect( yourobject, {depth:null} ));
and that will print the entire content.
Note that this is just an artifact of console.log printing it.