javascript - 严格比较在生产环境中未产生预期结果,但在本地服务器上运行良好

标签 javascript mysql vue.js switch-statement laravel-5.5

我目前在 JavaScript 中使用严格的 === 比较,该比较会根据我的计算机上的要求生成预期结果,但部署到服务器时却不会,其行为会有所不同。

脚本

computed: {
            type: function(){
                switch(this.job.type)
                {
                    case 1:
                        return 'Request Quote';
                    case 2:
                        return 'Negotiate';
                    case 3:
                        return 'Fixed';
                    default:
                        return 'matched none';
                }
            },
}

模板

<p class="text-primary"><strong>{{type}}</strong></p>

本地服务器上作业类型 2 的输出

Image showing Negotiate on Local Server

生产服务器上作业类型 2 的输出 Image showing matched none on Production Server

如果我将代码切换到宽松的比较 If 语句,它会很好地工作

如果语句

computed: {
        type: function(){
            if(this.job.type == 1) return 'Request Quote';
            else if(this.job.type == 2) return 'Negotiate';
            else if(this.job.type == 3) return 'Fixed';
            else return 'matched none';
        },
}

生产服务器中的结果 enter image description here

正如您可能已经注意到的,我正在使用 VUEJS 框架,作业对象也是使用 axios 获取的数据库模型。我还在后端使用 Laravel。

会不会是mysql版本的问题?

在本地计算机上运行的版本 Image showing MYSQL server version on Local Machine

生产环境中运行的版本 enter image description here

最佳答案

这意味着您的代码/配置/等。在生产中创建一个字符串,或一个数字对象(而不是基元),或某种其他类型的对象,当强制转换为数字时,该对象会强制转换为您的预期值之一。

带有字符串的示例:

function strict(type) {
    switch(type)
    {
        case 1:
            return 'Request Quote';
        case 2:
            return 'Negotiate';
        case 3:
            return 'Fixed';
        default:
            return 'matched none';
    }
}

function loose(type) {
    if(type == 1) return 'Request Quote';
    else if(type == 2) return 'Negotiate';
    else if(type == 3) return 'Fixed';
    else return 'matched none';
}

var n;
console.log("Number:");
n = 2;
console.log("strict", strict(n));
console.log("loose", loose(n));

console.log("String:");
n = "2";
console.log("strict", strict(n));
console.log("loose", loose(n));

数字对象示例:

function strict(type) {
    switch(type)
    {
        case 1:
            return 'Request Quote';
        case 2:
            return 'Negotiate';
        case 3:
            return 'Fixed';
        default:
            return 'matched none';
    }
}

function loose(type) {
    if(type == 1) return 'Request Quote';
    else if(type == 2) return 'Negotiate';
    else if(type == 3) return 'Fixed';
    else return 'matched none';
}

var n;
console.log("Primitive:");
n = 2;
console.log("strict", strict(n));
console.log("loose", loose(n));

console.log("Number object:");
n = new Number(2);
console.log("strict", strict(n));
console.log("loose", loose(n));

您必须找出环境/代码/配置中的不同之处,以便您最终得到错误类型的 this.job.type 值。

关于javascript - 严格比较在生产环境中未产生预期结果,但在本地服务器上运行良好,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48685099/

相关文章:

javascript - 使用 HTML 并排显示两个文档

Javascript - 如何读取文件

javascript - 如何使用 Vuejs 组件根据从 Laravel 传递的变量切换类

vue.js - Tailwind CSS : The `focus:border-2` class does not exist, 但 `focus:rotate-2` 确实

javascript - 可重用的 javascript 对象、原型(prototype)和范围

javascript - 如何传递 jslint 并将全局变量传递给我的 IIFE?

mysql - 如何重复使用不同数据集的报告?

mysql - 对于表中的每个不同 ID,计算不同表中相关 UID 的数量。 (SQL)

php - 如何选择具有最新日期时间的标题?

vue.js - 元素用户界面和 Font Awesome