【问题标题】:Why isn't my collection.upsert() query NOT setting multiple parameters to the collection?为什么我的 collection.upsert() 查询没有为集合设置多个参数?
【发布时间】:2019-01-15 17:47:14
【问题描述】:

我的 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!

我按是的

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 变量是set 到key 但查询忽略设置password、email 和names 变量。

有人能解释一下为什么集合中没有设置(包括)password、email 和 names 变量吗?

【问题讨论】:

  • 也许可以试试: APIKeys.update( { "owner": userId }, { $set: { "key": newKey, "password": password, "email": email, "names":名称 } , { upsert: true});
  • 当你说你从控制台检查是 mongo 控制台还是 chrome 控制台?如果最新的可能不是您的密码,则电子邮件和名称变量不会发布给用户。
  • 你有没有附加到APIKeys 的架构或任何之前的钩子?
  • @Ivo。我尝试了你的建议:` var keyId = APIKeys.update( { "owner": userId }, { $set: { "key": newKey, "password": password, "email": email, "names": names }, }, { upsert: true });` 但这仍然给出了相同的结果,只有键被保存到集合中。密码、电子邮件和姓名仍未保存。
  • @Ivo 它是我检查的 Chrome 控制台。您对未向用户发布的密码、电子邮件和名称变量的解释是有道理的。这似乎是一个愚蠢的问题,但是,我如何将变量发布给用户?

标签: javascript meteor collections upsert


【解决方案1】:

在下面的查询中只有键是upsert的原因:

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

实际上是错误的。 ENTIRE 查询正在正确执行。 问题出在发布配置本身!

发布配置只允许显示 key 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 为我指出了关于这个问题的正确方向。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-08
    • 1970-01-01
    • 2011-12-07
    • 1970-01-01
    • 1970-01-01
    • 2011-02-14
    • 2011-10-21
    相关资源
    最近更新 更多