Express: typeof(object.key) ==> return (Object) instead of String but it works on normal node.js environment perfectely

Created on 28 Jul 2017  路  4Comments  路  Source: expressjs/express

//the common object for  node.js ==> run time and 
var result =  {
            "merchant_id": 25,
            "id": 36,
            "name": "carpenter",
            "description": "incharge of all jobs",
            "designation": "supervisor",
            "salary": "1234",
            "image": "3a1e836b-32d3-4fac-807b-aa0ea1a962f9.jpg",
            "created_at": "2017-07-28T06:31:32.000Z",//this data is automatically generated by Sql DateTime
            "updated_at": null,
            "isActive": 0,
            "address": "Dubai"
        };
console.log(result.created_at); //===>2017-07-28T06:31:32.000Z throws object perfectly;
console.log(typeof(result.created_at));//===> throws an **object** ._>>seems some thing wrong_

in my console while running in express.js

i have tried with the node.js in the
client side by making that as a single file.js and run using node.js

//works perfectly
===>output
console.log(result.created_at); //===>2017-07-28T06:31:32.000Z throws object perfectly;
console.log(typeof(result.created_at));//===> throws an **string**  

I am using Node.js -version 8.2.1 and npm -v 5.2.3

question

Most helpful comment

Ah, yea @palanik got it. Since the mysql module will convert your TIMESTAMP values into JavaScript Date objects, so typeof(new Date()) is "object" in JavaScript. The dateStrings option in the mysql module can give you strings instead of objects if that is what you want :)

All 4 comments

Hi @Muthukumars1994 I'm not really sure what is going on. Can you provide a full reproduction case I can run to see this happening?

//hello @dougwilson ,
//I have been using Mysql with Express.js for my Application While cheking my Created_at Timestamp //typeof

var express   =  require('express'),
router       =  express.Router(),
mysql        =  require('mysql'),
mysql_settings = {
    host      : 'localhost',
    user      : 'name',
    password  : "password",
    database  : "dbname"
},
qb = require('node-querybuilder').QueryBuilder(mysql_settings, 'mysql');
router.post("/api/",    function(req, res) {
//i have tried both with node query builder and mysql raw query it throws the same 
    qb.select('name','mobile', 'created_at')
    .get('merchant',   function(err, result) {
        <h4 style ="color:red" >console.log(typeof(result[0].name));//string
            console.log(typeof(result[0].mobile));//number
                console.log(typeof(result[0].created_at));</h4>//object  ==> some thing went wrong while checking **timestamp**==> always throws **object**
        });
});

//*I have tried all use cases with Mysql created At *
//the below is the data type with field value in mysql
//_'created_at', 'timestamp', 'NO', '', 'CURRENT_TIMESTAMP', 'on update CURRENT_TIMESTAMP'_

@Muthukumars1994
By default, mysql returns TIMESTAMP/DATETIME/DATE fields as Date objects. You can override in the configuration with dateStrings: true to return as strings in the connection options.

From the docs:

dateStrings: Force date types (TIMESTAMP, DATETIME, DATE) to be returned as strings rather then inflated into JavaScript Date objects. Can be true/false or an array of type names to keep as strings. (Default: false)

Ah, yea @palanik got it. Since the mysql module will convert your TIMESTAMP values into JavaScript Date objects, so typeof(new Date()) is "object" in JavaScript. The dateStrings option in the mysql module can give you strings instead of objects if that is what you want :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

despairblue picture despairblue  路  3Comments

cuni0716 picture cuni0716  路  3Comments

zackarychapple picture zackarychapple  路  3Comments

nove1398 picture nove1398  路  3Comments

guyisra picture guyisra  路  3Comments