Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/411.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 返回值,其中不产生meteor_Javascript_Meteor - Fatal编程技术网

Javascript 返回值,其中不产生meteor

Javascript 返回值,其中不产生meteor,javascript,meteor,Javascript,Meteor,这可能是javascript 101,但我将非常感谢您的帮助。当返回空值时,我想在Meteor helper中返回一个不同的值。有什么想法吗 Template.singleComment.helpers({ momentTime: function () { return moment(this.createdAt).fromNow(); }, byWho: function(){ if(Meteor.users.findOne({_id: user}).username){

这可能是javascript 101,但我将非常感谢您的帮助。当返回空值时,我想在Meteor helper中返回一个不同的值。有什么想法吗

Template.singleComment.helpers({
momentTime: function () {
    return moment(this.createdAt).fromNow();
},
byWho: function(){
    if(Meteor.users.findOne({_id: user}).username){
        return this.username;
    } else {
        return "none"
    }
}

})

好的,这与在没有回调的情况下处理findOne的错误有关。这很有效

Template.singleComment.helpers({
momentTime: function () {
    return moment(this.createdAt).fromNow();
},
byWho: function(){
    user = Meteor.users.findOne({_id: this.createdBy})
    if(user){
        return user.username;
    } else {
        return this.createdBy;
    }
}
})

返回“none”
更改为您想要的值?您可以返回null;