JSON stringify将0转换为null

Mea*_*her 1 javascript node.js

我试图将我的对象转换为JSON但它不会将我的枚举转换为0.当我打印枚举时我得到0但是当我在对象内部使用它时它变为空.如果我使用字符串,而不是整数,它的工作原理.

(function () {
    'use strict';
    var ItemStatus = {
        'Prepared': 0,
        'Ongoing': 1,
        'Finished': 2
    };
    module.exports = ItemStatus;
})();

(function () {
    'use strict';
    var ItemStatus = require('./itemstatus');

    function ItemDetail(detail) {
        detail = detail || {};

        this.message = detail.message || null;
        this.location = detail.location || null;
        this.status = detail.status || null;
        this.date = detail.date || null;

    }

    module.exports = ItemDetail;
})();

(function () {
    'use strict';

    var ItemDetail = require('./itemdetail');
    var ItemStatus = require('./itemstatus');

    function Item(item) {

        item = item || {}

        this.name = item.name || null;
        this.details = item.details || [];
        this.isFinished = item.isFinished || null;
        this.finishDate = item.finishDate || null;

    }

    Item.prototype.addDetail = function(message, location,date,status) {

        if (this.isFinished) {
            this.isFinished = false;
        }

        console.log('Status: ' + status); //Prints 0 correctly
        var detail = new ItemDetail({
            message: message,
            location: location,
            date:date,
            status:status

        });

        this.details.push(detail);
        if (status === ItemStatus.Finished) {
            this.isFinished = true;
            this.finishDate = date;
        }
    };


    module.exports = Item;
})();
Run Code Online (Sandbox Code Playgroud)

失败的测试

var should = require('should');

var Item = require('../lib/models/item');
var ItemDetail = require('../lib/models/itemdetail');
var ItemStatus = require('../lib/models/itemstatus');


describe('Item Detail Test:', function() {
   this.enableTimeouts(false);


    var myItem = new Item({
        name: 'Something',
    });

    myItem.addDetail('Something happened','NY',1212122,ItemStatus.Prepared);
    myItem.addDetail('Another thing','NY',1412122,ItemStatus.Ongoing);
    myItem.addDetail('It is done','NY',1212122,ItemStatus.Finished);


    it('should print JSON', function() {
        myItem.name.should.eql('Something');
        console.log(myItem.details[0].status);
        myItem.details[0].status.should.eql(ItemStatus.Prepared);
        console.log(JSON.stringify(myItem));
    });
});
Run Code Online (Sandbox Code Playgroud)

打印时,我的项目显示以下内容

{"name":"Something","details":[{"message":"Something happened","location":"NY","status":null,"date":1212122},{"message":"Another thing","location":"NY","status":1,"date":1412122},{"message":"It is done","location":"NY","status":2,"date":1212122}],"isFinished":true,"finishDate":1212122}
Run Code Online (Sandbox Code Playgroud)

t.n*_*ese 5

您的问题与JSON stringify无关.

该行将this.status = detail.status || null;转换0null.
因为0是假的,你this.status将被设定nulldetail.status存在0.

您可以通过启动你解决这个问题ItemStatus1或者不使用this.status = detail.status || null;

所以要么使用:

var ItemStatus = {
    'Prepared': 1,
    'Ongoing': 2,
    'Finished': 3
};
Run Code Online (Sandbox Code Playgroud)

或者以这种方式进行测试:

this.status = detail.status;
if( this.status !== 0 && !this.status) {
  this.status = null;
}
Run Code Online (Sandbox Code Playgroud)