0

我正在尝试使用 firebase 函数将配置发送到物联网设备。这是我正在尝试使用的教程。这是我正在部署的 Node js 代码

本教程之后说,在部署后,我将对我的 firebase 数据库进行任何更改,它将作为设备配置发送到 iot 设备。

registryid 是 INTERNET_OF_BULBS。物联网设备名称为 Iot-device-1。

教程中的实际代码不适用于部署。所以我删除了导入模块部分并使用了require。本教程执行这些导入。

import * as admin from "firebase-admin";
import * as functions from 'firebase-functions';
import { runInDebugContext } from 'vm';
import { DeviceManager } from './devices';
how do i import { DeviceManager } from './devices';

所以我只做 2 个导入。否则,如果我在 index.js 中使用导入,它会在部署时出错。1:1 错误解析错误:'import' 和 'export' 可能只出现在 'sourceType: module' 下面是我的 index.js 文件。

const admin =require('firebase-admin');
const functions =require('firebase-functions');

admin.initializeApp();

// create a device manager instance with a registry id, optionally pass a region
const dm = new DeviceManager('INTERNET_OF_BULBS');
const firestore = admin.firestore();
// start cloud function
exports.configUpdate = functions.firestore
  // assumes a document whose ID is the same as the deviceid
  .document('device-configs/Iot-device-1')
  .onWrite((change,context) => {
    var beforeData = change.before.val(); // data before the write
    var afterData = change.after.val();
    if (context) {
      await dm.setAuth();
      console.log(context.params.deviceId);
      // get the new config data
      const configData = change.after.data();
      return dm.updateConfig(context.params.deviceId, configData);
    } else {
      throw(Error("no context from trigger"));
    }

  })

这是我尝试部署时外壳上的错误,上面的代码。

firebase deploy
=== Deploying to 'wifi-lights-88783'...
i  deploying functions
Running command: npm --prefix "$RESOURCE_DIR" run lint
> functions@ lint /home/suposhmehta/fire/functions
> eslint .
/home/suposhmehta/fire/functions/index.js
  17:13  error  Parsing error: Unexpected token dm
✖ 1 problem (1 error, 0 warnings)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! functions@ lint: `eslint .`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the functions@ lint script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR!     /home/suposhmehta/.npm/_logs/2018-11-10T09_38_15_585Z-debug.log
Error: functions predeploy error: Command terminated with non-zero exit code1

0 info it worked if it ends with ok
1 verbose cli [ '/usr/local/nvm/versions/node/v8.9.4/bin/node',
1 verbose cli   '/usr/local/nvm/versions/node/v8.9.4/bin/npm',
1 verbose cli   '--prefix',
1 verbose cli   '/home/suposhmehta/fire/functions',
1 verbose cli   'run',
1 verbose cli   'lint' ]
2 info using npm@5.6.0
3 info using node@v8.9.4
4 verbose run-script [ 'prelint', 'lint', 'postlint' ]
5 info lifecycle functions@~prelint: functions@
6 info lifecycle functions@~lint: functions@
7 verbose lifecycle functions@~lint: unsafe-perm in lifecycle true
8 verbose lifecycle functions@~lint: PATH: /usr/local/nvm/versions/node/v8.9.4/lib/node_modules/npm/node_modules/npm-lifecycle/node-gyp-bin:/home/suposhmehta/fire/functions/node_
modules/.bin:/home/suposhmehta/gopath/bin:/google/gopath/bin:/google/google-cloud-sdk/bin:/usr/local/go/bin:/opt/gradle/bin:/opt/maven/bin:/usr/local/sbin:/usr/local/bin:/usr/sbi
n:/usr/bin:/sbin:/bin:/usr/local/nvm/versions/node/v8.9.4/bin:/google/go_appengine:/google/google_appengine
9 verbose lifecycle functions@~lint: CWD: /home/suposhmehta/fire/functions
10 silly lifecycle functions@~lint: Args: [ '-c', 'eslint .' ]
11 silly lifecycle functions@~lint: Returned: code: 1  signal: null
12 info lifecycle functions@~lint: Failed to exec lint script
13 verbose stack Error: functions@ lint: `eslint .`
13 verbose stack Exit status 1
13 verbose stack     at EventEmitter.<anonymous> (/usr/local/nvm/versions/node/v8.9.4/lib/node_modules/npm/node_modules/npm-lifecycle/index.js:285:16)
13 verbose stack     at emitTwo (events.js:126:13)
13 verbose stack     at EventEmitter.emit (events.js:214:7)
13 verbose stack     at ChildProcess.<anonymous> (/usr/local/nvm/versions/node/v8.9.4/lib/node_modules/npm/node_modules/npm-lifecycle/lib/spawn.js:55:14)
13 verbose stack     at emitTwo (events.js:126:13)
13 verbose stack     at ChildProcess.emit (events.js:214:7)
13 verbose stack     at maybeClose (internal/child_process.js:925:16)
13 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:209:5)
14 verbose pkgid functions@
15 verbose cwd /home/suposhmehta/fire
16 verbose Linux 4.14.33+
17 verbose argv "/usr/local/nvm/versions/node/v8.9.4/bin/node" "/usr/local/nvm/versions/node/v8.9.4/bin/npm" "--prefix" "/home/suposhmehta/fire/functions" "run" "lint"
18 verbose node v8.9.4
19 verbose npm  v5.6.0
20 error code ELIFECYCLE
21 error errno 1
22 error functions@ lint: `eslint .`
22 error Exit status 1
23 error Failed at the functions@ lint script.
23 error This is probably not a problem with npm. There is likely additional logging output above.
24 verbose exit [ 1, true ]

我如何摆脱这些问题?请帮忙。

4

0 回答 0