【问题标题】:Meteor not updating sub document is not working流星不更新子文档不起作用
【发布时间】:2016-10-26 05:42:07
【问题描述】:

大家好,请告诉我为什么我不能更新我的子文档。我可以更新普通文档字段,但不能更新子文档,我在这里硬编码了方法,只是为了让它工作。

import { Mongo } from 'meteor/mongo';
import { check } from 'meteor/check';
import './Months.js';

export const Product = new Mongo.Collection('product');

ProductItem = new SimpleSchema({    
    author: {
        type: String,
        autoValue: function() {
            return this.userId
        },
        autoform: {
            type: "hidden"
        }
    },
    fileId: {
        type: String,
        autoValue: function() {
            return this._id
        },
        autoform: {
            type: "hidden"
        }
    },
    name: {
        type: String
    },
    sellingPrice: {
        type: Months,
        autoform: {
            type: "hidden"
        },
    },
    purchasePrice: {
        type: Months,
        autoform: {
            type: "hidden"
        },
    }
});

Product.allow({
    insert: function (userId, doc) {
        // the user must be logged in, and the document must be owned by the user
        return !!userId;
    },
    update: function (userId, doc) {
        // can only change your own documents
        return !!userId;
    },
    remove: function (userId, doc) {
        // can only remove your own documents
        return doc.owner === userId;
    },
    fetch: ['owner']
});


Meteor.methods({
    updateProduct: function(id, newVal, target, inner) {
        console.log(id + "" + target);
        Product.update(id, {
            $set: {'sellingPrice.$.M1': 10 },
        });
    },
});



Product.attachSchema( ProductItem );

我也试过了

Meteor.methods({
    updateProduct: function(id, newVal, target, inner) {
        console.log(id + "" + target);
        Product.update(id, {
            $set: {'sellingPrice.$': {'M1': 10} },
        });
    },
});

还有

Meteor.methods({
    updateProduct: function(id, newVal, target, inner) {
        console.log(id + "" + target);
        Product.update(id, {
            $set: {'sellingPrice.M1': 10 },
        });
    },
});

仍然注意到,nada。

从 'meteor/mongo' 导入 { Mongo }; 从“流星/检查”导入{检查};

Months = new SimpleSchema({
    M0: {
        type: Number,
        autoValue: function() {
            return 0;
        },
        autoform: {
            type: "hidden"
        },
    },
    M1: {
        type: Number,
        autoValue: function() {
            return 0;
        },
        autoform: {
            type: "hidden"
        },
    },
    M2: {
        type: Number,
        autoValue: function() {
            return 0;
        },
        autoform: {
            type: "hidden"
        }

    },
    M3: {
        type: Number,
        autoValue: function() {
            return 0;
        },
        autoform: {
            type: "hidden"
        }

    },
    M4: {
        type: Number,
        autoValue: function() {
            return 0;
        },
        autoform: {
            type: "hidden"
        }

    },
    M5: {
        type: Number,
        autoValue: function() {
            return 0;
        },
        autoform: {
            type: "hidden"
        }

    },
    M6: {
        type: Number,
        autoValue: function() {
            return 0;
        },
        autoform: {
            type: "hidden"
        }

    },
    M7: {
        type: Number,
        autoValue: function() {
            return 0;
        },
        autoform: {
            type: "hidden"
        }

    },
    M8: {
        type: Number,
        autoValue: function() {
            return 0;
        },
        autoform: {
            type: "hidden"
        }

    },
    M9: {
        type: Number,
        autoValue: function() {
            return 0;
        },
        autoform: {
            type: "hidden"
        }

    },
    M10: {
        type: Number,
        autoValue: function() {
            return 0;
        },
        autoform: {
            type: "hidden"
        }

    },
    M11: {
        type: Number,
        autoValue: function() {
            return 0;
        },
        autoform: {
            type: "hidden"
        }

    },

});

所以任何帮助都会非常感谢

【问题讨论】:

  • 能否添加 Months.js 架构?
  • 嘿@andresk 谢谢你的回复

标签: mongodb meteor


【解决方案1】:

从我在autoValue docs看到的情况来看,我相信你一个月都不能更新了,因为你在用:

autoValue: function() {
        return 0;
}

在架构上。这总是在更新时将值更改为 0。

所以你可以尝试删除它,看看它是否有效。

如果你想要一个默认值,你可以使用defaultValue

【讨论】:

    【解决方案2】:

    当您说您的 sellPrice 数据类型是 Months 时,您是在说您将存储其他类型为 Months 的对象的 ID。

    如果您已将月份存储在其他集合中,则必须以某种方式获取月份的 ID。但是如果您不需要存储月份,您可以将 Month 声明为具有属性的对象。 https://github.com/aldeed/meteor-simple-schema#schema-keys.

    此外,您可以使用 Autoform 并声明月份的模式,并且使用 quickForm 您将拥有开箱即用的插入和编辑操作。

    【讨论】:

    • 嘿,谢谢,当你说我可以使用 Autoform 并声明架构时,一百万听起来很棒,你是什么意思
    • 您只需要在创建集合ProductItem 的同一文件中创建架构Months = new SimpleSchema({;这样,Autoform 将呈现 Months 模式的字段,但无需声明 Months 的集合。
    猜你喜欢
    • 1970-01-01
    • 2017-06-08
    • 2017-04-18
    • 2016-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多