【问题标题】:How to check sequelize count and return Boolean value if count > 0如果计数> 0,如何检查续集计数并返回布尔值
【发布时间】:2017-01-09 04:03:26
【问题描述】:

我正在使用 Nodejs,我有一个查询来获取计数。我想检查 count > 0 是否返回 true,否则 - false。 但我无法使用 nodejs 处理它。 这是我的代码:

const uidIsConnection = (model, entityId, uid) =>
		models[model].count({
			where: { entityId, profileId: uid }
		});

var data = uidIsConnection('ProfileData', user.id, id);
//I want this variable data to have the value of true or false

现在,data 返回承诺:

Promise {
  _bitField: 2097152,
  _fulfillmentHandler0: undefined,
  _rejectionHandler0: undefined,
  _promise0: undefined,
  _receiver0: undefined,
  _boundTo: profiledata }

如何根据count > 0而不是promise返回布尔值?

【问题讨论】:

    标签: javascript node.js sequelize.js


    【解决方案1】:

    Sequelize count 返回一个 Promise,这是您的 data 正在解决的问题。要获得函数的实际计数,您应该这样做:

    const uidIsConnection = (model, entityId, uid) =>
            models[model].count({
                where: { entityId, profileId: uid }
            });
    
    uidIsConnection('ProfileData', user.id, id).then(count => {
        console.log('boolean version of count', !!count);
    });
    

    以后你可以使用asycawait来同步解决这个问题,但是这个功能不是widely supported yet

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-12
      • 1970-01-01
      • 1970-01-01
      • 2021-12-14
      • 2019-11-06
      • 2012-10-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多