【问题标题】:Mongoose: Null on nested propertyMongoose:嵌套属性为空
【发布时间】:2013-11-05 00:04:26
【问题描述】:

我不确定这是否正确,但 IMO 在输出 FALSE 时应该输出 TRUE。

我是不是做错了什么?

 var mongoose = require('mongoose');

 var NestedSchema = new mongoose.Schema({
     nestedProperty: {
         nestedValue: String
     }
}); 

var Nested = mongoose.model('Nested', NestedSchema);

var instance = new Nested();

instance.nestedProperty = null;

console.log(instance.nestedProperty == null);

【问题讨论】:

    标签: javascript node.js mongodb null mongoose


    【解决方案1】:

    值存储为(某种)字符串。

        console.log(instance.nestedProperty == "null"); // true
        console.log(typeof instance.nestedProperty  === "string"); // false
    

    【讨论】:

    • 好的。但这对于检查/断言/......这真的很烦人......它可以被认为是一个错误吗?
    • 确实很烦人。这与检查 "undefined" 而不是 undefined 完全相同。我会说您可以将其视为错误。但是,由于 nestedPropertystring 类型,因此其中可能有一些解释。如果没有任何 string 类型也将 null 转换为 string 之类的东西,那将是值得测试的。
    猜你喜欢
    • 1970-01-01
    • 2015-03-07
    • 2014-10-27
    • 2014-11-09
    • 1970-01-01
    • 2018-12-22
    • 2017-04-16
    • 2017-09-04
    • 1970-01-01
    相关资源
    最近更新 更多