Date not working correctly #716

Closed
n9 opened this Issue May 13, 2013 · 8 comments

Projects

None yet

3 participants

@n9
n9 commented May 13, 2013

I have hit a strange bug: Sequelize (an ORM for node.js) was not correctly handling Date columns. After long debugging I've realized that the bug is caused by node-webkit.

A date passed from user code (I assume from webkit) is no longer instanceof Date in a node module.

Here is a simple example to reproduce this bug:

index.html:

<html><body><script>
var date = new Date();
document.body.innerHTML = [date instanceof Date, date.constructor]
  + '<br/>' + (date.constructor === new Date().constructor)
  + '<br/><br/>' + require('test').testDate(date);
</script></body></html>

node_modules/test.js:

module.exports = {
  testDate: function(date) {
    var date2 = new Date();
    return [date instanceof Date, date.constructor]
      + '<br/>' + (date.constructor === new Date().constructor)
      + '<br/><br/>' + [date2 instanceof Date, date2.constructor]
      + '<br/>' + (date2.constructor === new Date().constructor);
  }
};

The output is:

true,function Date() { [native code] }
true

false,function Date() { [native code] }
false

true,function Date() { [native code] }
true

I am testing it in the version 0.5.1.

(#669 might be also caused by this bug.)

@n9
n9 commented May 13, 2013

#702 looks like a similar problem.

@rogerwang
Member

The reason is that the module code runs in Node context so the Date are different objects. You can find the same behavior in frames in browser.

@zjuwwq
zjuwwq commented May 14, 2013

you can check object using Object.prototype.toString.call(new Date()) === "[object Date]" to solve your problem

@n9
n9 commented May 14, 2013

The problem in my situation is that the I need to pass data between third party libraries. For instance, an UI library (in webkit) creates a Date and ORM library (in node.js) is checking whether the values is a Date.

Is there a support from node-webkit for that? What are the common best practices for that scenario?

@rogerwang
Member

@n9 can you override the Date object in DOM context with the one from Node context on app start? Would that work for you?

@n9
n9 commented May 15, 2013

Thanks, it works:)

Why is this not done automatically? Could this break something?

@rogerwang
Member

On Wed, May 15, 2013 at 7:18 PM, n9 notifications@github.com wrote:

Thanks, it works:)

Why is this not done automatically? Could this break something?

Because I wasn't aware of this issue. Now I'm not sure whether it should be
done in node-webkit.

@n9
n9 commented May 15, 2013

Ok, should this issue stay open?

@rogerwang rogerwang closed this Jun 18, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment