Mongoose对象中的虚拟是未定义的?

时间:2016-01-17 22:55:54

标签: node.js mongodb mongoose database-schema mongoose-schema

我最近一直在学习MongoDB和Mongoose以学习MEAN堆栈。使用本课程:https://www.edx.org/course/introduction-mongodb-using-mean-stack-mongodbx-m101x我正在尝试为我的产品架构创建一个虚拟,以更加用户友好的方式显示价格。但是,在打印出此虚拟displayPrice时,除非通过toObjecttoJSON进行访问,否则它将显示为未定义,即使这样,美元符号也会显示为问号。我为任何愚蠢和明显的忽视道歉,我是这个数据库的新手,并且找不到简单解释事情的许多教程。

以下是代码:

var mongoose = require("mongoose");
var Category = require("./categoryschema.js");

var productSchema = {
    name: {
        type: String,
        required: true
    },
    // Pictures must start with http://"
    pictures: [{ type: String, match: /^http:\/\//i }],
    price: {
        amount: {
            type: Number,
            required: true
        },
        currency: {
            type: String,
            enum: ["USD", "EUR", "GBP"],
            required: true
        }
    },
    category: Category.categorySchema
};

module.exports = new mongoose.Schema(productSchema);
module.exports.productSchema = productSchema;

var schema = new mongoose.Schema(productSchema);
var Product = mongoose.model("Product", schema);

var currencySymbols = {
    "USD": "$",
    "EUR": "E",
    "GBP": "P"
};

// Make human readable string form of price. "$25" instead of "25 USD"

schema.virtual("displayPrice").get(function() {
    return currencySymbols[this.price.currency] + "" + this.price.amount;
});

schema.set("toObject", { virtuals: true });
schema.set("toJSON", { virtuals: true });

var p = new Product({
    name: "test product",
    price: {
        amount: "33.58",
        currency: "USD"
    }
});

console.log(p.displayPrice);
p.price.amount = 20;
console.log(p.displayPrice);
p.price.currency = "EUR";
console.log(JSON.stringify(p));
var obj = p.toObject();
console.log(obj.displayPrice);

输出:

undefined
undefined
{"name":"test product","_id":"569c39774093336c149eba2c","category":{"ancestors":
[]},"price":{"amount":20,"currency":"EUR"},"pictures":[],"displayPrice":"E20","i
d":"569c39774093336c149eba2c"}
E20

1 个答案:

答案 0 :(得分:1)

似乎唯一有效的方法是将虚拟机分配给价格键。我不知道为什么这是解决方案,所以任何答案都将不胜感激。它似乎只是直接记录虚拟值的变化。

新产品样式:

var mongoose = require("mongoose");
var Category = require("./categoryschema.js");
var fx = require("./fx.js");

var productSchema = {
    name: {
        type: String,
        required: true
    },
    // Pictures must start with http://"
    pictures: [{ type: String, match: /^http:\/\//i }],
    price: {
        amount: {
            type: String,
            required: true,
            set: function(v) {
                this.internal.approximatePriceUSD = v / (fx()[this.price.currency] || 1);
                return v;
            }
        },
        currency: {
            type: String,
            enum: ["USD", "EUR", "GBP"],
            required: true,
            set: function(v) {
                this.internal.approximatePriceUSD = this.price.amount / (fx()[v] || 1);
                return v;
            }
        }
    },
    category: Category.categorySchema,
    internal: {
        approximatePriceUSD: Number
    }
};

var schema = new mongoose.Schema(productSchema);
var Product = mongoose.model("Product", schema);

var currencySymbols = {
    "USD": "$",
    "EUR": "E",
    "GBP": "P"
};

// Make human readable string form of price. "$25" instead of "25 USD"

schema.virtual("price.displayPrice").get(function() {
    return currencySymbols[this.price.currency] + "" + this.internal.approximatePriceUSD;
});

schema.set("toObject", { virtuals: true });
schema.set("toJSON", { virtuals: true });

var p = new Product({
    name: "test product",
    price: {
        amount: "12.50",
        currency: "USD"
    },
    category: {
        name: "test"
    }
});

console.log(p.price.displayPrice);
console.log(p.price.currency + ": " + p.internal.approximatePriceUSD);
p.price.currency = "EUR";
console.log(p.price.displayPrice);
console.log(p.price.currency + ": " + p.internal.approximatePriceUSD);
p.price.currency = "GBP";
console.log(p.price.displayPrice);
console.log(p.price.currency + ": " + p.internal.approximatePriceUSD);

module.exports = new mongoose.Schema(productSchema);
module.exports.productSchema = productSchema;

新输出:

$12.5
USD: 12.5
E11.363636363636363
EUR: 11.363636363636363
P8.333333333333334
GBP: 8.333333333333334