代码之家  ›  专栏  ›  技术社区  ›  SirBT

为什么不是我的收藏。upsert()查询是否未为集合设置多个参数?

  •  1
  • SirBT  · 技术社区  · 6 年前

    我有一个 collection.upsert() 在我的Meteor方法中起作用。出于某种奇怪的原因,它似乎只向上插入第一个参数,而忽略向上插入其余参数。有人能解释一下为什么会这样吗?

    在事件模板下方找到 userId, password, email, names, 参数被传递到Meteor方法 regenerateApiKey 主题

    /客户/主要客户。js

    Template.apiKey.events({
      'click .regenerate-api-key': function( ){
    
       var userId = Meteor.userId(); 
       var password = Meteor.user().services.google.accessToken;
       var email = Meteor.user().services.google.email;
       var names = Meteor.user().services.google.name;
    
       alert("Password: " +password);
       alert("email: " +email);
       alert("email: " +names);     
    
       confirmRegeneration = confirm( "Are you sure? This will invalidate your current key!" );
    
         if ( confirmRegeneration ) {
           Meteor.call( "regenerateApiKey", userId, password, email, names, function( error, response ) {
             if ( error ) {
               alert( error.reason, "danger" );
             } else {
      +response );
               alert( "All done! You have a new API key: " +response );
               console.log("Response is: " +response);
             }
           });
         }
      }
    });
    

    上面的事件模板呈现5个弹出警报:

    弹出窗口。1. Password: ya29.Glz

    弹出窗口。2. email: centos.east@gmail.com

    弹出窗口。3. email: Centos East

    弹出窗口。4. Are you sure? This will invalidate your current key!

    我按“是”

    弹出窗口。5. All done! You have a new API key: [object Object]

    下面的代码说明了 Meteor.call( "regenerateApiKey") 以及 userId, password, email, names 参数。

    /服务器/主服务器。js

    Meteor.methods({
      regenerateApiKey: function( userId, password, email, names ){
        check( userId, Meteor.userId() );
    
        var newKey = Random.hexString( 32 );
        var password = password;
        var email = email;
        var names = names;
    
        console.log("password: " +password);
        console.log("email: " +email);
        console.log("names: " +names );
        console.log("newKey: " +newKey);
    
        try {
          var keyId = APIKeys.upsert( { "owner": userId }, {
            $set: {
              "key": newKey,
              "password": password, 
              "email": email, 
              "names": names
            }
          });
    
          return keyId;
        } catch(exception) {
            console.log("FAILED UPDATE")
          return exception;
        }
      }
    });
    

    在终端中,我可以看到上面的代码呈现了什么:

    password: ya29.Glz
    
    email: centos.east@gmail.com
    
    names: Cent East
    
    newKey : 337829bb18082690a32f94a3c23b3782
    

    当我询问 APIKeys.find().fetch() 在控制台中,我得到:

    key: "337829bb18082690a32f94a3c23b3782"
    _id:"PgBmn6zSYiXTbx6tu"
    

    这表明只有 newKey 变量是 set key 但查询被忽略,无法设置 password , email names 变量。

    有人能解释一下为什么 暗语 , 电子邮件 名字 集合中没有设置(包括)变量?

    1 回复  |  直到 6 年前
        1
  •  1
  •   SirBT    6 年前

    为什么只有钥匙 upsert 在下面的查询中:

    APIKeys.upsert( { "owner": userId }, {
            $set: {
              "key": newKey,
              "password": password, 
              "email": email, 
              "names": names
            }
          });
    

    事实上是假的。整个查询的执行是正确的。 故障出在发布配置本身!

    发布配置只允许 钥匙 fields 要显示的值! 以下是我的发布配置代码:

    Meteor.publish( 'APIKey', function(){
      var user = this.userId;
      var data = APIKeys.find( { "owner": user }, {fields: 
                                                        {  
                                                          "key": true, 
                                                        } });
    
    
      if ( data ) {
            return data;
      }
    
      return this.ready();
    });
    

    要更正此问题,我必须将我的发布重新配置为以下代码:

    Meteor.publish( 'APIKey', function(){
      var user = this.userId;
      var data = APIKeys.find( { "owner": user }, {fields: 
                                                        {  
                                                          "key": true, 
                                                          "password": true,
                                                          "email": true,
                                                          "names": true
    
                                                        } });
    
    
      if ( data ) {
         return data;
      }
    
      return this.ready();
    });
    

    我想感谢帖子中的@Ivo为我指出了这个问题的正确方向。