Fetch results from model in Waterline if property is defined

做~自己de王妃 提交于 2019-12-10 14:22:03

问题


I've got a model where not every property is required. I'd like to query the model and return all instances where the property is defined.

Here's what I think the code should look like, but it doesn't work. Any ideas or links to some detailed documentation?

MyModel.find()
.where({
    "propertyThatMayExist" : {
        "!=" : undefined
    }
});

Thanks a bunch in advance!


回答1:


The easiest way would be to test against null. The correct operator is ! or not:

MyModel.find().where({propertyThatMayExist: {'!': null}}).exec(console.log);

This assumes you don't want to sometimes explicitly set the property to null for an instance, which would be problematic for some databases anyway (think of MySQL, which defaults most fields to NULL if they aren't filled out).



来源:https://stackoverflow.com/questions/22772742/fetch-results-from-model-in-waterline-if-property-is-defined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!