1 meteor
我正在构建一个messenger应用程序,在创建对话之前,我想验证用户是否存在.如果是,那么它将创建对话.如果没有,那么它应该返回一个错误.我一直在服务器端使用此代码但由于某种原因它将无法正常工作.我尝试了很多不同的调整,但这基本上是我的结构:
Meteor.methods({
createConversation: function(secondPerson) {
function doesUserExist(secondPerson) {
var userx = Meteor.users.findOne({username: secondPerson});
if (userx === secondPerson) {
return false;
} else {
return true;
}
}
if (doesUserExist()) {
Conversations.insert({
person1: Meteor.user().username,
person2: secondPerson
});
} else {
Conversations.insert({
person1: "didn't work"
});
}
}
});
Run Code Online (Sandbox Code Playgroud)
您缺少的要点是find返回游标,而findOne返回文档.以下是实现该方法的一种方法:
Meteor.methods({
createConversation: function(username) {
check(username, String);
if (!this.userId) {
throw new Meteor.Error(401, 'you must be logged in!');
}
if (Meteor.users.findOne({username: username})) {
return Conversations.insert({
person1: Meteor.user().username,
person2: username
});
} else {
throw new Meteor.Error(403, username + " does not exist!");
}
}
});
Run Code Online (Sandbox Code Playgroud)
请注意以下功能:
username是一个字符串id新会话的内容要使用它,只需打开浏览器控制台并尝试拨打电话:
Meteor.call('createConversation', 'dweldon', function(err, id){console.log(err, id);});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3970 次 |
| 最近记录: |