Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Date attribute exists #592

Closed
kussberg opened this issue May 15, 2015 · 20 comments
Closed

Date attribute exists #592

kussberg opened this issue May 15, 2015 · 20 comments

Comments

@kussberg
Copy link

I have declared a date field in the model:

    "availableToDate": {
        "type": "date"
    },

and then tried to make a find with an or condition:

or: [{
          availableToDate: {gte: today}
        }, {
          availableToDate: {exists: false}
        }, {
          availableToDate: null
        }],

and i get following error:

Error: Invalid date: [object Object]
    at DateType (/Users/edgarkuskov/Documents/Foodo Workspace/foodo-api-server/node_modules/loopback-datasource-juggler/lib/dao.js:929:11)
    at Function.DataAccessObject._coerce (/Users/edgarkuskov/Documents/Foodo Workspace/foodo-api-server/node_modules/loopback-datasource-juggler/lib/dao.js:1082:15)
    at Function.DataAccessObject._coerce (/Users/edgarkuskov/Documents/Foodo Workspace/foodo-api-server/node_modules/loopback-datasource-juggler/lib/dao.js:982:16)
    at Function.DataAccessObject._normalize (/Users/edgarkuskov/Documents/Foodo Workspace/foodo-api-server/node_modules/loopback-datasource-juggler/lib/dao.js:922:8)
    at Function.find (/Users/edgarkuskov/Documents/Foodo Workspace/foodo-api-server/node_modules/loopback-datasource-juggler/lib/dao.js:1184:10)
    at Object.eval [as forward] (eval at recompile (/usr/local/lib/node_modules/strongloop/node_modules/strong-agent/lib/proxy.js:237:15), <anonymous>:4:32)
    at Function.find (eval at wrap (/usr/local/lib/node_modules/strongloop/node_modules/strong-agent/lib/proxy.js:193:20), <anonymous>:3:21)
    at Function.Product.findByCategory (/Users/edgarkuskov/Documents/Foodo Workspace/foodo-api-server/common/models/product.js:31:13)
    at SharedMethod.invoke (/Users/edgarkuskov/Documents/Foodo Workspace/foodo-api-server/node_modules/loopback/node_modules/strong-remoting/lib/shared-method.js:207:25)
    at HttpContext.invoke (/Users/edgarkuskov/Documents/Foodo Workspace/foodo-api-server/node_modules/loopback/node_modules/strong-remoting/lib/http-context.js:335:12)

Is this a bug in the validation?

@raymondfeng
Copy link
Contributor

It seems the Date type validation is not happy with null, which is a bug. Can you submit a patch?

@kussberg
Copy link
Author

@raymondfeng i was just testing it out and unfortunately don't have a fix for that

@kblcuk
Copy link

kblcuk commented May 19, 2015

Probably related? strongloop/loopback#1009 (comment)

@kussberg
Copy link
Author

kussberg commented Jun 9, 2015

Is there a fix already for this "exists" problem with date? @raymondfeng @bajtos @ritch
I think this is the code that could work:

function DateType(arg) {
  if (arg == null) {
    return null;
  } else if(typeof arg === 'object' && typeof arg.getTime === 'function') {
    if (isNaN(arg.getTime())) {
      throw new Error('Invalid date: ' + arg);
    }
    return arg;
  } else if(typeof arg === 'string') {
    var d = new Date(arg);
    if (isNaN(d.getTime())) {
      throw new Error('Invalid date: ' + arg);
    }
    return d;
  } else {
    throw new Error('Invalid date: ' + arg);
  }
}

@bajtos
Copy link
Member

bajtos commented Jun 9, 2015

@kuskov Could you please submit a pull request and include tests to demonstrate that the proposed solution works as intended? You can take a look at #130 for inspiration.

@kussberg
Copy link
Author

kussberg commented Jun 9, 2015

@bajtos unfortunately i don't really know how all this pull request/contributing stuff works...

@bajtos
Copy link
Member

bajtos commented Jun 9, 2015

@kuskov don't worry, we'll help you. Start by reading my Git Intro, it should set you up for making your first contribution. Let me know if there is anything not clear enough, so that I can explain in more detail.

@kussberg
Copy link
Author

kussberg commented Jun 9, 2015

@bajtos i have fetched the fork for me, changed the file and pushed it to master on the fork. Somehow i don't understand what to do next?

@bajtos
Copy link
Member

bajtos commented Jun 10, 2015

@kuskov create a feature branch, it will make your life much easier. Starting on master:

# create feature branch 
git checkout -b fix-date-exists
# push to the server
git push -u origin fix-date-exists
# switch back to master
git checkout master
# reset back to the original revision from upstream
git reset --hard HEAD^
# force-push to overwrite the history
git push -f

Once you have that done, open https://github.com/kuskov/loopback-datasource-juggler in your browser, you should see a white stripe suggesting you to submit a pull request.

@tsanjoto
Copy link

any new updates on this issue?

@mamartins
Copy link

yeah news on this PR would be great

@dhmlau
Copy link
Member

dhmlau commented Apr 21, 2017

I believe we've fixed it in PR: #1339
@ekussberg , could you please verify? If you're good with the fix, we can close this issue. Thanks.

@dhmlau dhmlau self-assigned this Apr 21, 2017
@dhmlau dhmlau added the apex label Apr 21, 2017
@dhmlau
Copy link
Member

dhmlau commented May 1, 2017

Closing this since we think it's fixed in PR #1339. If you still encounter this problem, please reopen. Thanks.

@dhmlau dhmlau closed this as completed May 1, 2017
@dhmlau dhmlau added this to the Sprint 35 - Apex milestone May 1, 2017
@daankets
Copy link

daankets commented Oct 4, 2017

This issue is still not fixed using version 3.12.0 of the datasource juggler.

Upon using a query like this:

{where: {someDateField: {exists: false}}}

Loopback still productes the Error: Invalid date: [object Object] error. This, combined with the lack of a 'not' operator makes it impossible to query for documents that do not have a certain date field set.

@bajtos
Copy link
Member

bajtos commented Oct 5, 2017

@daankets thank you for the comment, I am reopening this issue then. Would you like to contribute the fix yourself?

@bajtos bajtos reopened this Oct 5, 2017
@bajtos bajtos unassigned 0candy and dhmlau Oct 5, 2017
@bajtos bajtos removed this from the Sprint 35 - Apex milestone Oct 5, 2017
@daankets
Copy link

daankets commented Oct 5, 2017 via email

@stale
Copy link

stale bot commented Dec 4, 2017

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Dec 4, 2017
@stale
Copy link

stale bot commented Dec 18, 2017

This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository.

@stale stale bot closed this as completed Dec 18, 2017
@daankets
Copy link

daankets commented Mar 8, 2018

@0candy This is still an issue today. If you are using MongoDB at least, you can not query using the exists operator on a date field.

{where: {someDateField: {exists:true}}} ==> Error: Invalid date: [object Object]

@sigalor
Copy link

sigalor commented Dec 2, 2021

@bajtos I am still experiencing this issue with MongoDB in Loopback 4 as well. Any updates on this in the last few years?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests