簡體   English   中英

如果count> 0,如何檢查連續計數並返回布爾值

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

我正在使用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返回布爾值,而不是諾言?

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

將來您可以使用asyc await同步解決此問題,但是該功能尚未得到廣泛支持

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM