我的问题如下。流星:插入产生多个条目
我开始使用流星,并写了一些针对猫鼬的小代码。
我想将用户插入到我的数据库中。我首先必须用meerkatId的用户名称呼叫meerkat,然后才能获取用户信息。
这是我的代码。
Users = new Mongo.Collection("users");
if (Meteor.isClient) {
Template.body.helpers({
users: function() {
// Otherwise, return all of the tasks
return Users.find({});
}
});
Template.user.helpers({
parentUsername: function (parentContext) {
return parentContext.info.username;
}
});
Template.body.events({
"submit .user-search": function (event) {
// This function is called when the new task form is submitted
event.preventDefault();
Meteor.call("removeAllUsers");
Meteor.call("searchUser", $("#username").val());
//event.target.text.value = "";
// Prevent default form submit
return false;
}
});
Template.user.events({
"click .getUserBroadcast": function() {
// Set the checked property to the opposite of its current value
Meteor.call("getUserBroadcasts", this._id, this.meerkatId);
}
});
}
if (Meteor.isServer) {
Meteor.startup(function() {
// code to run on server at startup
Users.remove({});
});
}
Meteor.methods({
searchUser: function (username) {
HTTP.call("PUT", "https://social.meerkatapp.co/users/search?v=2",
{data: {"username": username}},
function (error, result) {
if (!error) {
content=JSON.parse(result.content);
Meteor.call("getUserInfos", content.result[0]);
}
});
},
getUserInfos: function(meerkatId){
HTTP.call("GET", "https://resources.meerkatapp.co/users/"+meerkatId+"/profile?v=1.0",
{},
function (error, result) {
if (!error) {
contentJson=JSON.parse(result.content);
console.log(contentJson.result);
Users.insert(contentJson.result);
}
});
},
removeAllUsers: function() {
return Users.remove({});
}
});
有人能告诉我为什么这个解决了3个相同的用户条目(_id的当然不是),而得到的控制台输出只有一次?
当我在searchUser方法中插入时,我得到2个条目。我相信这是因为异步回调。
我在做什么错?