简体   繁体   English

SimpleSchema更新错误

[英]SimpleSchema update error

I'm trying to create a favorite button by saving the usersId to the logged in users account. 我正在尝试通过将usersId保存到登录的用户帐户来创建收藏夹按钮。 The concept is, if there is a userId (user is a favorite), else user is not a favorite. 这个概念是,如果有一个userId(用户是收藏夹),则用户不是收藏夹。 The problem is I'm getting an error update failed: Error: Favorites must be an array and I'm not sure what this means. 问题是我update failed: Error: Favorites must be an array ,我不确定这意味着什么。

Path: schema.js 路径: schema.js

Schema.UserProfile = new SimpleSchema({
    "favorites.$.favorite": {
        type: Object
    }
});

Path: studentlist.js 路径: studentlist.js

Template.student.events({
'click .favourite':function(event,template) {
      console.log('click');
        var candidateId = this._id;

        Meteor.users.update({_id: Meteor.userId() }, { $set: { "profile.favorites": candidateId }});
    }
});

You have basically two errors. 您基本上有两个错误。

as of the error, you must have defined Favorites property to be an array. 从错误开始,您必须已将“ Favorites属性定义为数组。 and in the code you're trying to update with $set command. 并在代码中尝试使用$set命令更新。

when you're inserting an item into an array in MongoDB, you've to use $push operator. 在将项目插入MongoDB中的数组时,必须使用$push运算符。

and the second problem you'll face after fixing this one would be the improper data type insertion. 解决此问题后,您将面临的第二个问题是数据类型插入不正确。 because you've defined favorite to be an object, but trying to insert a mere id. 因为您已将favorite定义为一个对象,但是尝试插入一个id。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM