为什么我的collection.upsert()查询没有为集合设置多个参数?

时间:2018-08-08 12:42:50

标签: javascript meteor collections upsert

我的Meteor方法中有一个collection.upsert()函数。由于某些奇怪的原因,它似乎仅对第一个参数进行升序,而忽略对其余参数进行升序。有人可以解释一下为什么会这样吗?

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

/client/main.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个弹出警报:

POPUP.1 Password: ya29.Glz

POPUP.2 email: centos.east@gmail.com

POPUP.3 email: Centos East

POPUP.4 Are you sure? This will invalidate your current key!

我按YES

POPUP.5 All done! You have a new API key: [object Object]

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

/server/main.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变量是setkey,但查询被忽略了以设置passwordemailnames变量。

有人能解释一下为什么集合中没有设置passwordemailnames变量的原因吗?

1 个答案:

答案 0 :(得分:1)

在以下查询中,只有键是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在主题中为我指出有关此问题的正确方向。