检查javascript列表中的字符串是否不工作(在hyperledger中)

检查javascript列表中的字符串是否不工作(在hyperledger中),javascript,blockchain,hyperledger,Javascript,Blockchain,Hyperledger,所以,我想用javascript检查一个广告是否显示在一个允许的网站上。我使用了那篇文章中解释的方法 这是我的密码: async function ImpCertification(tx) { Impression = await getAssetRegistry('org.example.basic.Impression') const source = Impression.PrintedSite whitelist = ["Youtube", "Google"

所以,我想用javascript检查一个广告是否显示在一个允许的网站上。我使用了那篇文章中解释的方法

这是我的密码:

async function ImpCertification(tx) {

    Impression = await getAssetRegistry('org.example.basic.Impression')

    const source = Impression.PrintedSite

    whitelist = ["Youtube", "Google", "Facebook", "Twitter"]

    if (whitelist.indexOf(source) < 0) {
        // Checks if source in whitelist
        throw new Error ('This impression does not respect branding')
    }

    // Checks every necessary conditions to validate impression
    if (whitelist.indexOf(source) >=0) {
        // Save the old value of the asset.
        const oldValue = Impression.Valid;

        // Update the asset with the new value.
        Impression.Valid = true;

        // Get the asset registry for the asset.
        const assetRegistry = await getAssetRegistry('org.example.basic.transaction.ValidateImpression');
        // Update the asset in the asset registry.
        await assetRegistry.update(Impression);

        // Emit an event for the modified asset.
        let event = getFactory().newEvent('org.example.basic', 'Validation');
        event.asset = tx.asset;
        event.oldValue = oldValue;
        event.newValue = true;
        emit(event);
    }
    await null
}
异步功能认证(tx){ Impression=Wait getAssetRegistry('org.example.basic.Impression')) const source=Impression.PrintedSite 白名单=[“Youtube”、“Google”、“Facebook”、“Twitter”] if(白名单索引of(源)<0){ //检查源是否在白名单中 抛出新错误('此印象不尊重品牌') } //检查每一个必要的条件以确认印象 if(白名单.索引(源)>=0){ //保存资产的旧值。 const oldValue=Impression.Valid; //使用新值更新资产。 印象。有效=真实; //获取资产的资产注册表。 const assetRegistry=wait getAssetRegistry('org.example.basic.transaction.ValidateImpression'); //更新资产注册表中的资产。 等待资产登记更新(印象); //为修改的资源发出事件。 让event=getFactory().newEvent('org.example.basic','Validation'); event.asset=tx.asset; event.oldValue=oldValue; event.newValue=true; 发射(事件); } 等待无效 } 我正在开发一个hyperledger业务网络,因此某些部分可能看起来很奇怪,但我必须让您注意白名单/索引。虽然我知道它的逻辑,但它就是不起作用

我试图在Impression.PrintedSite中输入白名单的每一个元素,但无论是否正确,它每次都会抛出“此Impression不尊重品牌”错误


在你问之前,我检查了Impression.PrintedSite中的大写字母,它是一个字符串,我尝试了建议使用布尔和“includes”的另一种方法,但它就是不起作用。停

首先,确保传递给
indexOf
的字符串是实际字符串:

typeof=='string'

要检查列表中是否有字符串,我建议使用ES6
Array.prototype.includes()

['a','b','c'].包括('b')

要检查字符串是否是列表中字符串的子字符串,请将
Array.prototype.includes()
方法与
string.prototype.includes()
方法组合:

(我还在那里使用ES6
reduce
来简化表达式)


['hello','world','my','name','is','johny','cash'].reduce((当前,项目)=>current | |项目。包括('substring|u to|u search'),false)

长话短说,由于函数中没有填充对象“impression”的ID,常量只是空的,因为获取的信息不存在

因此,由于null不在白名单中,indexof返回-1,每次程序都会进入该条件并返回错误

这个简单的解决方法是

async function ImpCertification(ValidateImpression) {
    imp = ValidateImpression.impression
    const source = imp.PrintedSite

    ect...

}
其中,“ValidateImpression”是通过ID指向Impression资产的事务

asset Impression identified by ImpID {
    o String ImpID
    o String PrintedSite
}    

transaction ValidateImpression {
        --> Impression impression
}

使用
whitelist.includes(source)
而不是
indexOf
我认为问题在于源变量。正如您在这个代码笔中看到的,如果我硬编码列表中的一个值,它似乎会起作用:您是否尝试过(whitelist.indexOf(String(source))>=0)?@inumov17我尝试过,但没有起作用:/@PrzemekMarcinkiewicz,同样的问题谢谢您的洞察力,不幸的是,我发现Hyperledger结构运行时不支持ES6。编辑:我检查了一个旧的开发日志,我的坏,ES6从一月份就开始支持了。我现在正在深入研究