More details below, but the question is is the below a valid javascript object and how do I access that first array type thing
{ [some-text-here-without-quotes] key1: 'text', key2: 1, key3: '23000', key4: 0 }
Building an app with Nodejs, Express and MySQL.
I have decided to use the 'mysql' library found in NPM at this link: https://www.npmjs.com/package/mysql.
While testing I purposely created a duplicate entry condition that violated a unique constraint because I wanted to see what the returned error object looks like.
When I console.log(error) from the query the following is what prints:
{ [Error: ER_DUP_ENTRY: Duplicate entry 'abcdefg' for key 'token_UNIQUE'] code: 'ER_DUP_ENTRY', errno: 1062, sqlState: '23000', index: 0 }
I must be missing something obvious. But I cannot seem to access the data in that first array looking thing. The part of the object that starts with [Error: and ends with 'token_UNIQUE']
How do I access that part of the object?
(also, first I thought it was an improperly formatted object string or something buggy from the mysql library but underscore.js says it is an object
_.isObject(error); //true
got the exact same problem, and i still dont understand what that particular syntax means..
however, i managed to access this part of the object by force casting it into a string, by merely concatenating it to a string contant :
if (error) {
error.isError=1
errmsg=" "+error <---- cast to string here
error.msg=errmsg; <---- then back into the object as a tring
response.end(JSON.stringify(error));
db.end();
console.log("<-"+error)
return;
}
You can do
Object.keys(err)
This will give you the list of keys that are contained in the err object. From that, you can pick the keys of your intereste. (or) you can use,
console.dir(err)
to print the object in a formatted way.
maybe the answer is a little bit late, I encountered this problem yesterday. but the solution is simple, just use toString()
error.toString()
error.message maybe a formal way
both methods above returning the same output
Related
I fetch an array that is stored in mongoose with ObjectIds. After I output this array through the console I get the following output:
["5a5f7199105cc908e874d74b","5a5f9199105cc908e874d74b"]
At the same time fetch a different object from mongoose. If I output the ObjectId of this object through the console (object.id) I get the following output:
5a5f7199105cc908e874d74b
I try to check whether the value is contained in the array or not. My code looks like this:
myArray.includes(myObject.id)
and I expect to have the result of true. Unfortunately, I get false as a result even though The value is in the array. Maybe it has to deal with the OBject Type? I really do not know how to solve this. Thanks.
Because in case of Array ["5a5f7199105cc908e874d74b","5a5f9199105cc908e874d74b"] - typeof values is String, so you operate with strings, while when you operate with object property(myObject.id) - you, probably, operate with ObjectID, which is default for latest MongoDBs. Anyway, we need to see your code and know your DB version etc. Easiest way to check is run code "typeof myObject.id", and if you see not "string" - this is an answer to your question.
article is an object variable.
console.log("reesult id " + JSON.stringify(article));
When I do this, it outputs me: [{"Id":43}]
and when I do:
console.log(article[0]);
It outputs me {"Id":43}
but now... HOW to get just 43?
Because when I type:
console.log(article[0].Id);
, It returns me undefined instead of 43. Pf.
So, HOW to get 43?
It is very difficult because I made researches and it does not work as well.
I am unable to comment to so posting as an answer.
Your solution looks fine.
Could you print the output for
JSON.stringify(article[0].id)
Sequelize has it's own way how to return objects in json so you should try using their way to handle with objects since you are using that library.
This is from Sequelize documentation.
For any response object that resulted from a query, you can extract
only the data you want by appending .get({plain:true}) the the
response.
Here you have a link to that documentation so you can read more for that since you are not providing any code to us.
This error throws up whenever I enter a duplicate entry in mysql.
{ [Error: ER_DUP_ENTRY: Duplicate entry 'sample#gmail.com' for key 'email
'] code: 'ER_DUP_ENTRY', errno: 1062, sqlState: '23000', index: 0 }
What I want to do is turn this Error object into a string. I tried using JSON.stringify() and when I printed it on the console, only the last part got converted into a string :
{"code":"ER_DUP_ENTRY","errno":1062,"sqlState":"23000","index":0}
I need to convert the first part as well of the error, the one inside the [ ] so that I am able to diagnose duplicates properly. How do I retrieve that part whenever I convert an Error object into a string?
I thought I had to use JSON for this problem because of the way the Error object was formatted. How I solved this one was just to use
err.toString()
To get the full stack use stack property of the Error object:
console.log(error.stack)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/stack
For future reference, just in case you wish to extract the Error messsage. Use error.message to extract the message. Here is a reference: https://nodejs.org/api/errors.html#errors_error_message
I'm writing a custom console.error function so that every time an error occurs I receive an e-mail. To send the error in e-mail body I use JSON.stringify(). The problem is that it is missing some properties. See the two images below:
Email:
In console:
And here is how I use JSON.stringfy:
JSON.stringify(arguments, null, 4);
I've been googling and found people with the same issue, but so far no answer.
Could you help me, please?
Edit : See this.
Since your Error object is inside another object, you might have to use 2 stringify calls :
JSON.stringify({
0: ...,
1: JSON.stringify({errorObject, ["message", "arguments", "type", "name"]}),
});
Or something like that.
If I'm getting this correctly, the informations you are lacking are in the Error object. My guess would be that JSON.stringify calls .toString() for each object inside it. Though, for an Error object, maybe the toString() function doesn't return ALL the informations you want, versus what you see in the console.
Maybe you'll have to call the Error object's .description() function yourself.
I have an IndexedDB of changes. I add an item like this, and then log the result to check the key has been created successfully:
_this._idb.add('steps', step).done(function (items) {
var item = items[0];
_logger.log("ADDED STEP", { id: item.__id__, step: item }, "CT");
});
The output from this is as expected:
...as you can see, the id has been added to the object when it is stored.
However, when I query the db to getback a list of objects, using this code:
this._idb.steps.query('timestamp').bound(start, end).execute().done(function (results) {
_logger.log("Results", results, "CT");
}
I don't get the id as part of the object that is returned:
... and the lack of id makes updating and deleting impossible.
How can I get the id of the item when I query indexed db using db.js - or am I approaching this in the wrong way, and is there something else I should be doing?
(Note: I'm using TypeScript to compile the JS, but I don't think that's especially relevant to this question)
This is expected behaviour, you're only going to get the __id__ property if you don't define a keyPath in your db schema.
Because there's no keyPath defined the value is not associated with it in indexeddb, it's only added to the resulting object after it has been added, because at that point in time we know the auto-incremented value that IndexedDB has assigned to it.
Since the value isn't really part of the object I don't have any way to assign it to the object when it comes out during a query, maybe I could use the position in the array but that's more likely to be wrong than right.
If you want the ID to be persisted against the object then you need to define a keyPath as part of the object store schema and the property will be added to the resulting object and available and it will be on the object returned from a query.
Disclaimer - I wrote db.js
Looking at the source, __id__ is only defined when your keyPath is null in the add() method. From what I'm seeing, you'll never see this in a query() response.
In IDB null keyPaths are allowed only when using auto-incrementing ("out-of-line") keys. So if you're getting the object back, it should have an auto-incrementing key on it or some other keyPath.
The __ prefix in JavaScript usually means the developer intended it to be a "private" property. I'm guessing this is for internal use and you shouldn't be counting on this in your application code.
Consider using explicit, so-called "in-line" keys on your object store.
The goal of db.js is easy and simple to use. Your is advanced use case.