0

我正在尝试使用 .json 文件填充一些函数参数,但是当我尝试在 Near 协议中构建合同时,命令窗口返回错误 TS6054。我正在从同一个目录导入 .json 并使用“import * as data from ../contract/assembly/sensor_data.json” 我已经修改了 tsconfig.json 并包含了 compileOperations: "resolveJsonModule": true,"esModuleInterop":真的。但这仍在继续发生。我还能做什么?

这是我的代码:

import { PersistentSet, PersistentUnorderedMap } from "near-sdk-as";
 import * as data from '../../contract/assembly/sensor_data.json';
 
@nearBindgen
export class Sensor {
    sensorName: string;
    sensorType: string;
    ownerId: string;
    allowedUsers: PersistentSet<string>;
    userRequests: PersistentSet<string>;

    constructor(
        _ownerId: string,
        _sensorName: string,
        _sensorType: string,) {
        this.ownerId = _ownerId;
        this.sensorName = _sensorName;
        this.sensorType = _sensorType;
        this.allowedUsers = new PersistentSet<string>(this.sensorName + "A");
        this.userRequests = new PersistentSet<string>(this.sensorName + "U");
    }
    getArgs(): string {
        return "{}";
    }

    hasAccess(accountId: string): bool {
        return this.allowedUsers.has(accountId) || accountId ==  this.ownerId;
    }

    getRequests(): string {
        let array: Array<string> = this.userRequests.values();
        return array.join(",");
    }

    addRequest(accountId: string): void {
        this.userRequests.add(accountId);
    }
}

export class DHT11 extends Sensor{
id : number;
date: string;
temp: f32;
hum : f32
constructor(
    sensorName:string,
    sensorType:string,
    ownerId:string,
    _id : number,
    _date:string, 
    _temp:f32, 
    _hum: f32){
    super(sensorName,sensorType,ownerId)
    let n= 1
    for (n ; n < data.Data.length; n++) {
        const DHT11 = data.Data[n];
    this.id = DHT11.id;
    this.date = DHT11.date;
    this.temp = DHT11.temperature;
    this.hum = DHT11.humidity;
    }
    
}
getArgs(): string {
    return "{Dispositivo DHT11. Sensor que mide temperatura y humedad}";
}
getState(): string {
    
    return "{id:" + this.id.toString() + ", Date:"
    + this.date.toString() + ", Temperature:" + this.temp.toString()
    + ", Humidity:" + this.hum.toString() + "}";
}
}

export let sensorRegistry = new PersistentUnorderedMap<string, Sensor>("s");
export let DHT11Registry = new PersistentUnordered

地图<string, DHT11>("d");

4

1 回答 1

0

我认为您正在尝试使用像 TypeScript 这样的 AssemblyScript,但您不能像那样直接导入 JSON。

相反,您可以考虑使用本示例includeBytes中使用的帮助程序将 JSON 加载到内存中,然后使用以下库之一加载/解析它:

注意,我自己没有尝试过,只是在这一点上的猜测

于 2021-11-24T08:19:37.843 回答