ERROR TS6054: File '../contract/assembly/sensor_data.json.ts' not found

229 Views Asked by At

I am trying to fill some function arguments with a .json file but when i try to build the contract in the Near Protocol the command window returns Error TS6054. I am importing the .json from the same dir with "import * as data from ../contract/assembly/sensor_data.json" I already modified the tsconfig.json and incuded the compileOperations: "resolveJsonModule": true,"esModuleInterop": true. But this still keeps happening. What else could i do?

This is my code:

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

Map<string, DHT11>("d");

1

There are 1 best solutions below

0
On

I think you are trying to use AssemblyScript like TypeScript but you can't directly import JSON like that.

Instead you could consider loading the JSON into memory using the includeBytes helper as used in this example and then loading / parsing it using one of these libraries:

Heads up, I haven't tried this myself, just a guess at this point