[英]METEOR: Fail to store data from external api
我需要存儲注冊用戶的IP數據。 插入新的注冊用戶后,我正在使用telize api檢索IP和collection hooks
以進行更新。
服務器/methods.js
Meteor.methods({
// The method expects a valid IPv4 address
'geoIp': function () {
// Construct the API URL
this.unblock();
var apiUrl = 'http://www.telize.com/geoip/';
// query the API
var response = HTTP.get(apiUrl);
Meteor.users.update(
{_id: this.userId},
{$set: {ipdata: response}}
)
}
});
lib / schemas.js
ipdata: {
type: Object,
optional: true
}
服務器/hooks.js
Meteor.users.after.insert(function (userId, doc) {
Meteor.call('geoIp');
});
這些代碼不會導致錯誤,可以成功注冊新用戶,但是無法存儲ipdata
有人發現我的代碼有什么問題嗎?
非常感謝你..
我很確定在這種情況下未設置this.userId
。 您是在服務器端而不是從另一個方法的主體中調用Meteor.call()
,因此被調用的方法沒有上下文。
相反,您可能只想為此使用一個常規函數。 例如
addGeoIp = function(userId, ip){
var response = HTTP.get(apiUrl);
Meteor.users.update(userId, {$set: {ipdata: response}});
}
Meteor.users.after.insert(function (userId, doc) {
addGeoIp(userId, ip);
});
而且,正如Mark所說, http://www.telize.com/geoip/本身只會返回服務器的IP,而不是客戶的IP。 因此,您需要將客戶端的IP傳遞到API中以獲取位置。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.