[英]How do I check if data exists in node express app
am working on a node / express app and currently trying to check if some session data exists or not.我正在开发一个节点/快速应用程序,目前正在尝试检查是否存在某些 session 数据。
To call the session data I'm currently using {{ if data['username'] }} which works fine.要调用 session 数据,我目前正在使用 {{ if data['username'] }} 工作正常。 To user a conditional if the data exists the following seems to work:
如果数据存在,要使用条件,以下似乎可行:
{% if data['username'] %} do this {% endif %}
However I'm not sure how to do something if it DOESN'T exist.但是,如果它不存在,我不确定该怎么做。 Such as
如
{% if data['username'] != '' %}
Any ideas appreciated!任何想法表示赞赏! Thanks
谢谢
I'm assuming you're using Nunjucks here based on the templating, and with that you can do:我假设您根据模板在这里使用 Nunjucks,并且您可以这样做:
{% if not data['username'] %}
{% endif %}
It depends on the templating language you're using.这取决于您使用的模板语言。 If it's Twig you can use
not
or empty
如果是 Twig 你可以使用
not
或empty
{% if not data['username'] or data['username'] is empty %}
Do this
{% endif %}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.