Исправление "ReferenceError: навигатор не определен" - PullRequest
0 голосов
/ 27 мая 2019

Я сейчас учусь делать PWA.Теперь я застрял в работнике службы.В логах сказано, что навигатор не определен.Как мне определить навигатор?

Я читал, что навигатор это ... функция в браузере?cmiiw.Тогда я не должен ничего устанавливать на свое приложение.Я попытался переместить код на свою целевую страницу (home.ejs), в мою точку входа (index.js), но ничего не получилось.Я также сделал файл sw.js в корневой папке.Я также прочитал учебник от Google , но не могу разобраться с этим.

Вот журнал

5:03:23 AM web.1 |  > sakamichi-akb-chords@1.0.0 start /home/emil/Documents/Chord PWA
5:03:23 AM web.1 |  > node index.js
5:03:23 AM web.1 |  /home/emil/Documents/Chord PWA/index.js:5
5:03:23 AM web.1 |  if ('serviceWorker' in navigator) {
5:03:23 AM web.1 |                         ^
5:03:23 AM web.1 |  ReferenceError: navigator is not defined
5:03:23 AM web.1 |      at Object.<anonymous> (/home/emil/Documents/Chord PWA/index.js:5:24)
5:03:23 AM web.1 |      at Module._compile (module.js:652:30)
5:03:23 AM web.1 |      at Object.Module._extensions..js (module.js:663:10)
5:03:23 AM web.1 |      at Module.load (module.js:565:32)
5:03:23 AM web.1 |      at tryModuleLoad (module.js:505:12)
5:03:23 AM web.1 |      at Function.Module._load (module.js:497:3)
5:03:23 AM web.1 |      at Function.Module.runMain (module.js:693:10)
5:03:23 AM web.1 |      at startup (bootstrap_node.js:188:16)
5:03:23 AM web.1 |      at bootstrap_node.js:609:3
5:03:23 AM web.1 |  npm
5:03:23 AM web.1 |   ERR!
5:03:23 AM web.1 |   Linux 4.15.0-50-generic
5:03:23 AM web.1 |  npm ERR! 
5:03:23 AM web.1 |  argv "/usr/bin/node" "/usr/bin/npm" "start"
5:03:23 AM web.1 |  npm ERR! node v8.10.0
5:03:23 AM web.1 |  npm ERR! npm  v3.5.2
5:03:23 AM web.1 |  npm ERR! code ELIFECYCLE
5:03:23 AM web.1 |  npm ERR! sakamichi-akb-chords@1.0.0 start: `node index.js`
5:03:23 AM web.1 |  npm ERR! Exit status 1
5:03:23 AM web.1 |  npm ERR! 
5:03:23 AM web.1 |  npm ERR! Failed at the sakamichi-akb-chords@1.0.0 start script 'node index.js'.
5:03:23 AM web.1 |  npm ERR! Make sure you have the latest version of node.js and npm installed.
5:03:23 AM web.1 |  npm ERR! If you do, this is most likely a problem with the sakamichi-akb-chords package,
5:03:23 AM web.1 |  npm ERR! not with npm itself.
5:03:23 AM web.1 |  npm ERR!
5:03:23 AM web.1 |   Tell the author that this fails on your system:
5:03:23 AM web.1 |  npm ERR!     node index.js
5:03:23 AM web.1 |  npm ERR!
5:03:23 AM web.1 |   You can get information on how to open an issue for this project with:
5:03:23 AM web.1 |  npm ERR!     npm bugs sakamichi-akb-chords
5:03:23 AM web.1 |  npm ERR! Or if that isn't available, you can get their info via:
5:03:23 AM web.1 |  npm 
5:03:23 AM web.1 |  ERR!     npm owner ls sakamichi-akb-chords
5:03:23 AM web.1 |  npm ERR! There is likely additional logging output above.
5:03:23 AM web.1 |  npm
5:03:23 AM web.1 |   ERR! Please include the following file with any support request:
5:03:23 AM web.1 |  npm ERR!     /home/emil/Documents/Chord PWA/npm-debug.log
[DONE] Killing all processes with signal  SIGINT
5:03:23 AM web.1 Exited with exit code null

А вот мой индекс.JS

const path = require ('path')
const PORT = process.env.PORT || 5000
const express = require('express')

if ('serviceWorker' in navigator) {
  window.addEventListener('load', function() {
    navigator.serviceWorker.register('/sw.js').then(function(registration) {
      // Registration was successful
      console.log('ServiceWorker registration successful with scope: ', registration.scope);
    }, function(err) {
      // registration failed :(
      console.log('ServiceWorker registration failed: ', err);
    });
  });
}

express()
    .use(express.static(path.join(__dirname, 'public')))
    .set('views', path.join(__dirname, 'views'))
    .set('view engine', 'ejs')
    .get('/', (req, res) => res.render('pages/home'))
    .listen(PORT, () => console.log(`Listening on ${ PORT }`))

Любой помогает?Краткое объяснение будет оценено.Большое спасибо!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...