如何在登录时拒绝user.profile的更新?

时间:2016-12-11 22:18:37

标签: meteor

在我的用户个人资料架构中,我有一个名为“actu”的字段,如下所示:

var getStreetView = function(newLat, newLong, callback) {
  var api_key = process.env.API_KEY;
  var url = 'https://maps.googleapis.com/maps/api/streetview?' +
    'size=' + 400 + 'x' + 400 +
    '&location=' + newLat + ',' + newLong +
    '&fov=' + 120 +
    '&pitch=' + 10 +
    '&key=' + api_key;

  request(url, function(error, response, body) {
    if (error) {
      console.log('getStreetView Error: ', error);
    } else {
      callback(body); //same error when using response.body
    }
  });
};

// REQUEST HANDLING
app.get('/city', function(req, res) {
  City.findOneRandom(function(err, result) {
    if (err) {
      console.log('Cities.findOneRandom Error: ', err);
    } else {
      var name = result.name;
      var country = result.country;
      var lat = result.latitude;
      var long = result.longitude;

      randomizeLocation(lat, long, function(newLat, newLong) {
        getStreetView(newLat, newLong, function(image) {
          res.writeHead(200, {
            'City': name,
            'Country': country,
            'Content-Type': 'image/jpeg'
          });

          res.end(image);
        });
      });
    }
  });
});

如果我重新启动Meteor,则不会更新此字段。对我来说很好。 但是当我登录我的帐户时,该字段会自动更新。对我来说错了 - 你明白了......

简而言之,我希望这个领域只在更新集合时才会被更新(通过autoform - 这很有用)。

有什么想法吗?任何欧比旺克诺比都帮助我?

编辑//这是我的查询和我的完整架构

//查询

Actu: {
 type: Date,
    autoValue: function() {
        if ( this.isUpdate ) {
            return new Date;
        } 
    }
}

//完整架构

{{#autoForm collection='Meteor.users' doc=currentUser type='update' id='accountForm'}}
<span>phone</span> {{> afFieldInput name='profile.phone'}}<br>
<span>avatar</span> {{> afFieldInput name='profile.avatar'}}
<button type='submit' class="at-btn dark">Update</button>
{{/autoForm}}

2 个答案:

答案 0 :(得分:1)

正在修改用户记录,因此this.updatetrue。您想控制哪个更新触发了此日期的设置。例如,您可以将更新链接到profile字段的更改,如下所示:

Actu: {
 type: Date,
    autoValue: function() {
        if ( this.field('profile').isSet ) {
            return new Date;
        } 
    }
}

只需将profile更改为您想要的任何字段作为控制字段。

答案 1 :(得分:1)

根据你的编辑补充迈克尔的答案:

您将profile架构定义为子架构,因此它不知道自己被用作用户架构的profile属性。

选择siblingField超过field这是一个很好的用例,因为field应该表示&#34;绝对&#34;主模式中的字段名称。

你可以更新autoValue字段iff任何&#34;触发&#34;使用以下行中的内容设置兄弟字段:

Actu: {
    type: Date,
    autoValue: function () {
        const triggerFields = ['phone', 'avatar'];
        let shouldTrigger =
            triggerFields.findIndex(f => this.siblingField(f).isSet) !== -1;

        if (shouldTrigger) {
            return new Date();
        }
    },
}

注意:

可以使用field(),但它不太灵活,因为您应该使用绝对符号指定它,例如,phone应指定为profile.phone,这意味着您如果您希望它起作用,则只能将其用作名为profile的字段的子模式。