You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将本地数据加载至JS变量?IoT模拟器数据加载报错求助

Fixing "Unexpected reserved word" Error for IoT Hub Simulator Data Loading

Problem Overview

You're building an IoT device simulator that sends sensor readings to IoT Hub, and you tried splitting your data loading logic into a separate file using ES6 import/export syntax. But when importing the function in your main JS file, you hit this error:

JS function failure, {"Message":"Line 14: Unexpected reserved word","FullName":"Jint.Parser.ParserException"}

Root Cause

The error happens because the runtime environment (using Jint for parsing) doesn't support ES6 module syntax (import/export). These keywords are reserved in ES6, but Jint (often configured for ES5 compatibility in IoT Hub contexts) doesn't recognize them, leading to a parsing failure.

Solutions

Here are a few practical fixes depending on your environment:

1. Embed Data Directly in the Main File (Simplest for Small Datasets)

Skip module imports entirely by moving your data logic directly into the main script. This avoids any module syntax issues:

// Main simulator file
function getData() {
  var data = { "example": [999, 999] };
  return data.example;
}

var data = getData(); 
data.forEach(function(e){ 
  state.pressure = e; 
  updateState(state); 
  log("Pressure increased to " + state.pressure); 
  sleep(1000); 
})

2. Use CommonJS Syntax (If Environment Supports It)

If your runtime allows CommonJS (Node.js-style modules), replace ES6 import/export with module.exports and require:
HavenEventData.js:

function getData() { 
  var data = { "example": [999, 999] }; 
  return data.example; 
}

// Export using CommonJS syntax
module.exports.getEventData = function() { 
  return getData(); 
};

Main file:

// Import using require instead of import
var eventDataModule = require("./HavenEventData");
var data = eventDataModule.getEventData();

data.forEach(function(e){ 
  state.pressure = e; 
  updateState(state); 
  log("Pressure increased to " + state.pressure); 
  sleep(1000); 
})

Note: Double-check if your IoT Hub simulator environment supports CommonJS—some restricted runtime environments only allow global-scope code.

3. Load Data from a Local JSON File (If File Access Is Allowed)

If your simulator runs in an environment that permits file system access (like a Node.js-based simulator), you can read directly from a JSON file instead of using a helper function:
First, create a pressureData.json file with your readings:

[999, 999]

Then update your main file:

var fs = require('fs');
// Read and parse the JSON file
var data = JSON.parse(fs.readFileSync('./pressureData.json', 'utf8'));

data.forEach(function(e){ 
  state.pressure = e; 
  updateState(state); 
  log("Pressure increased to " + state.pressure); 
  sleep(1000); 
})

Important: IoT Hub's built-in device simulator may restrict file system access, so this only works if you're running a custom Node.js-based simulator.

Key Note

Always verify the JavaScript version support for your target environment. Many IoT Hub script runtimes default to ES5 compatibility, so sticking to ES5 syntax (avoiding import/export, let/const if needed) will prevent parsing errors like this.

内容的提问来源于stack exchange,提问作者Stephenie Liu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 03:41:20