简体   繁体   English

如果count> 0,如何检查连续计数并返回布尔值

[英]How to check sequelize count and return Boolean value if count > 0

I'm using Nodejs and I have a query to get count. 我正在使用Nodejs,并且有一个查询来获取计数。 I want to check if count > 0 to return true, else - false. 我想检查count > 0是否返回true,否则返回false。 But I can't handle it using nodejs. 但是我无法使用nodejs处理它。 Here's my code: 这是我的代码:

 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 

Now, data returns promise: 现在, data将返回承诺:

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

How to return boolean value depending on count > 0 , not promise? 如何根据count > 0返回布尔值,而不是诺言?

Sequelize count returns a Promise, which is what your data is resolving to. 顺序count返回一个Promise,这就是您的data要解析的内容。 To get the actual count of the function you should do: 要获取函数的实际计数,您应该执行以下操作:

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);
});

In the future you could use asyc await to resolve this synchronously, but the feature is not widely supported yet . 将来您可以使用asyc await同步解决此问题,但是该功能尚未得到广泛支持

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM