微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Uncaught (in promise) TypeError: Assignment to constant variable in JavaScript File

如何解决Uncaught (in promise) TypeError: Assignment to constant variable in JavaScript File

我正在尝试将 TensorFlow 模型加载到 javascript 文件中,但我从一行中收到了一个未捕获的 TypeError,但不明白为什么我会收到该错误

这是产生错误代码

const demosSection = document.getElementById('demos');

const MODEL_FILE_URL = 'models/Graph/model.json';

// For Keras use tf.loadLayersModel().
const model = tf.loadGraphModel(MODEL_FILE_URL);

// Before we can use COCO-SSD class we must wait for it to finish
// loading. Machine Learning models can be large and take a moment to
// get everything needed to run.
model.then(function(loadedModel) {
    MODEL_FILE_URL = loadedModel;
    // Show demo section Now model is ready to use.
    demosSection.classList.remove('invisible');
});

这是控制台错误消息 - letsscan1.js:12 Uncaught (in promise) TypeError: Assignment to constant variable. at letsscan1.js:12

解决方法

const 类型变量是 final 类型。一旦这个变量被赋值,它的值就不能改变。

您在这里覆盖了它。

const demosSection = document.getElementById('demos');

let MODEL_FILE_URL = 'models/Graph/model.json'; // <-- this variable gets assigned another value below. Changed it to let

// For Keras use tf.loadLayersModel().
const model = tf.loadGraphModel(MODEL_FILE_URL);

// Before we can use COCO-SSD class we must wait for it to finish
// loading. Machine Learning models can be large and take a moment to
// get everything needed to run.
model.then(function(loadedModel) {
    MODEL_FILE_URL = loadedModel; // <-- you were reassigning a value to a const variable here.
    // Show demo section now model is ready to use.
    demosSection.classList.remove('invisible');
});

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。