gpt4 book ai didi

javascript - 从 HTTP.get 访问变量

转载 作者:行者123 更新时间:2023-11-28 07:06:00 25 4
gpt4 key购买 nike

我正在尝试访问 Meteor Accounts.onCreateUser 用户创建函数中名为 city 的变量,但我的尝试没有成功。我尝试在 HTTP.get 调用结束时返回 city ,并且还尝试在 HTTP.get 之外创建 city var 并简单地设置 city 而不使用 var 但这些事情似乎都不起作用。当 console.log(city) 运行时,它确实准确地输出所需的信息,因此这个变量一定不是问题。如果我犯了一个小错误,请原谅我。

Accounts.onCreateUser( function (options, user) {

if (options.profile) {

options.profile.picturelrg = "http://graph.facebook.com/" + user.services.facebook.id + "/picture/?type=large";
user.profile = options.profile;
options.profile.picturesm = "http://graph.facebook.com/" + user.services.facebook.id + "/picture/?type=small";
options.profile.messenger = "https://www.messenger.com/t/" + user.services.facebook.id;


HTTP.get("http://ipinfo.io", function (error, result) {
var place = JSON.parse(result.content);
var city = place.city;
console.log(city);

});


options.profile.city = city;


}

return user;

});

最佳答案

从技术上来说,正确的做法是您需要使用 HTTP.get 的同步版本。请参阅docsthis question例如。

但是还有一个更根本的问题:您试图获取用户的位置数据,但 onCreateUser 仅在服务器上运行。因此,即使您确实解决了这个问题,您最终也会在每个用户的个人资料中得到相同的数据。您需要在客户端上运行 get 并通过方法进行更新。尝试这样的事情:

Accounts.createUser(options, function(err) {
if (!err) {
HTTP.get(..., function(err, result) {
var city = ...;
var place = ...;
Meteor.call('updateProfileWithCityandPlace', city, place);
});
}
});

Meteor.methods({
updateProfileWithCityandPlace: function(city, place) {
check(city, String);
check(place, String);

Meteor.users.update(this.userId, {
$set: {'profile.city': city, 'profile.place': place}
});
}
});

关于javascript - 从 HTTP.get 访问变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31712845/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com