流星自动将地理位置保存到用户文档中

meteor automatically save geolocation to user document

本文关键字:用户文档 保存 地理位置 流星      更新时间:2023-09-26

我想自动跟踪页面上的用户。他们的位置应该在mongoDB中进行反应性更新。因此,我将在用户文档中添加一个新的属性"位置"。mdg:geolocation包已经为浏览器提供的位置数据提供了一个反应源:

Geolocation.latLng();

然而,当职位发生变化时,我很难更新用户文档。如何触发

Meteor.users.update(Meteor.userId(), { $set: ...});

数据库更新?我尝试了以下操作,但没有成功:

 Meteor.startup = function()
{
    Tracker.autorun(function () {
        var location = Geolocation.latLng();
        console.log(location);
        Meteor.users.update(Meteor.userId(), {
            $set: {
                   location: {
                        type: 'Point' ,
                        coordinates: [location.lng,  location.lat]
                   }
            }
        });
    });
}

现在我看到了错误。不是:

 Meteor.startup = function(){};

这是

 Meteor.startup(function(){});

来源http://docs.meteor.com/#/full/meteor_user:

默认情况下,服务器发布用户名、电子邮件和配置文件(可由用户写入)。

所以我认为你必须简单地更改你的代码:

        Meteor.users.update(Meteor.userId(), {
            $set: {
                   profile.location: {
                        type: 'Point' ,
                        coordinates: [location.lng,  location.lat]
                   }
            }
        });