Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uncaught ReferenceError: usersArray is not defined in Pings true #150

Open
yunierescobar opened this issue Feb 26, 2019 · 6 comments
Open

Comments

@yunierescobar
Copy link

yunierescobar commented Feb 26, 2019

Hello!

Thank you very much for this fantastic plugin. I have managed almost everything to work, I only have a problem with the Pings function. When the active, when sending the comment, I receive the error "Uncaught ReferenceError: usersArray is not defined"

This is my code:

                            $(function () {
                                var saveComment = function (data) {
                                    // Convert pings to human readable format
                                    $(data.pings).each(function (index, id) {
                                        var user = usersArray.filter(function (user) {
                                            return user.id == id
                                        })[0];
                                        data.content = data.content.replace('@' + id, '@' + user.fullname);
                                    });
                                    return data;
                                }
                                $('#comments-container').comments({

                                    noCommentsText: 'Aún no hay comentarios en esta foto. ¡Comenta ahora!',
                                    textareaPlaceholderText: '¿Qué te parece esta fotografía? Deja un comentario...',
                                    newestText: 'Más Recientes',
                                    oldestText: 'Más Antiguos',
                                    popularText: 'Más Popular',
                                    sendText: 'Enviar',
                                    replyText: 'Responder',
                                    youText: 'Tú',
                                    editedText: 'Editado',
                                    editText: 'Editar',
                                    deleteText: 'Eliminar',
                                    saveText: 'Guardar',
                                    viewAllRepliesText: 'Mostrar todas las respuestas (__replyCount__)',
                                    hideRepliesText: 'Ocultar respuestas',
                                    roundProfilePictures: true,
                                    enableHashtags: true,
                                    enablePinging: false,
                                    profilePictureURL: '<?php echo $AvatarImg;?>',
                                    maxRepliesVisible: 3,
                                    defaultNavigationSortKey: 'oldest',

                                    getUsers: function (success, error) {
                                        $.ajax({
                                            type: 'get',
                                            dataType: 'json',
                                            contentType: 'application/json',
                                            url: 'get_users.php',
                                            success: function (usersArray) {
                                                success(usersArray)
                                            },
                                            error: error
                                        });
                                    },

                                    getComments: function (success, error) {
                                        $.ajax({
                                            type: 'get',
                                            dataType: 'json',
                                            contentType: 'application/json',
                                            url: 'get_comments.php?photo=<?php echo $id;?>',
                                            success: function (commentsArray) {
                                                success(commentsArray)
                                            },
                                            error: error
                                        });
                                    },

                                    postComment: function (data, success, error) {
                                        $.ajax({
                                            type: 'post',
                                            dataType: "json",
                                            url: 'models/post_comments.php?pid=<?php echo $id;?>&cuid=<?php echo $UserId;?>&username=<?php echo $LoggedUsername;?>',
                                            data: data,
                                            success: function () {
                                                success(saveComment(data))
                                            },
                                            error: error
                                        });
                                    },

                                    putComment: function (data, success, error) {
                                        $.ajax({
                                            type: 'put',
                                            dataType: "json",
                                            url: 'models/put_comments.php?pid=<?php echo $id;?>&id=' + data.id,
                                            data: data,
                                            success: function () {
                                                success(saveComment(data))
                                            },
                                            error: error
                                        });
                                    },

                                    deleteComment: function (commentJSON, success, error) {
                                        $.ajax({
                                            type: 'post',
                                            url: 'models/delete_comments.php?pid=<?php echo $id;?>&id=' + commentJSON.id,
                                            success: success,
                                            error: error
                                        });
                                    },

                                    upvoteComment: function (commentJSON, success, error) {

                                        if (commentJSON.user_has_upvoted) {
                                            $.ajax({
                                                type: 'post',
                                                dataType: "json",
                                                url: 'models/post_upvotes.php?action=post&uid=<?php echo $UserId;?>&pid=<?php echo $id;?>',
                                                data: {
                                                    comment: commentJSON.id
                                                },
                                                success: function () {
                                                    success(commentJSON)
                                                },
                                                error: error
                                            });
                                        } else {
                                            $.ajax({
                                                type: 'post',
                                                dataType: "json",
                                                url: 'models/post_upvotes.php?action=delete&uid=<?php echo $UserId;?>&pid=<?php echo $id;?>',
                                                data: {
                                                    comment: commentJSON.id
                                                },
                                                success: function () {
                                                    success(commentJSON)
                                                },
                                                error: error
                                            });
                                        }
                                    },

                                    hashtagClicked: function (hashtag) {
                                        location = 'tags-' + hashtag + '-1'
                                    },

                                    timeFormatter: function (time) {
                                        moment.locale('es');
                                        return moment.utc(time, 'YYYY-MM-DD HH:mm:ss').local().fromNow();
                                    }

                                });
                            });
@Rohitgowda
Copy link

Hi,
You can try initializing users array by adding "var usersArray = new Array();" before the "var saveComment = function (data) {}" and it should work properly.

@yunierescobar
Copy link
Author

Hello! Thanks @Rohitgowda for you answer. I made the change you mentioned, but I get this error:

"Uncaught TypeError: Cannot read property 'fullname' of undefined"...

:(
I think that var usersArray is not taking the values returned by "getUsers" in json.
Any new ideas? I'm a novice in this javascript!
Millions of thanks!

@Rohitgowda
Copy link

Hi,
If that's the case try doing this changes in the get users AJAX success funtion. Then it should work fine

success:function(userArray){ //Changes the name here
usersArray = userArray; //Added new assignment
success(usersArray);
},

@yunierescobar
Copy link
Author

Magnificent @Rohitgowda ! Thank you so much for your help. Now the complement is working perfectly in all its functions. I hope it serves those who seek the same information. Regards!

@yunierescobar
Copy link
Author

yunierescobar commented Mar 11, 2019

This is de final code:

                        $(function () 
                                var userArray = [];
                                var saveComment = function (data) {

                                    $(data.pings).each(function (index, id) {
                                        var user = userArray.filter(function (user) {
                                            return user.id == id
                                        })[0];
                                        data.content = data.content.replace('@' + id, '@' + user.fullname);
                                    });
                                    return data;
                                };

                                $('#comments-container').comments({

                                    //Textos en español
                                    noCommentsText: 'Aún no hay comentarios en esta foto. Sé el primero el comentar',
                                    textareaPlaceholderText: '¿Qué te parece esta fotografía? Deja un comentario...',
                                    newestText: 'Más Recientes',
                                    oldestText: 'Más Antiguos',
                                    popularText: 'Más Popular',
                                    sendText: 'Enviar',
                                    replyText: 'Responder',
                                    youText: 'Tú',
                                    editedText: 'Editado',
                                    editText: 'Editar',
                                    deleteText: 'Eliminar',
                                    saveText: 'Guardar',
                                    viewAllRepliesText: 'Mostrar todas las respuestas (__replyCount__)',
                                    hideRepliesText: 'Ocultar respuestas',

                                    //Configuraciones
                                    roundProfilePictures: true,
                                    profilePictureURL: '<?php echo $AvatarImg;?>',
                                    maxRepliesVisible: 3,
                                    defaultNavigationSortKey: 'oldest',
                                    enableHashtags: true,
                                    enablePinging: true,
                                    readOnly: <?php echo $readOnly;?>,

                                    getUsers: function (success, error) {
                                        $.ajax({
                                            type: 'get',
                                            dataType: 'json',
                                            async: false,
                                            url: 'get_users.php',
                                            success: function (usersArray) {
                                                userArray = usersArray;
                                                success(userArray)
                                            },
                                            error: error
                                        });
                                    },

                                    getComments: function (success, error) {
                                        $.ajax({
                                            type: 'get',
                                            dataType: 'json',
                                            url: 'get_comments.php?photo=<?php echo $id;?>',
                                            success: function (commentsArray) {
                                                success(commentsArray)
                                            },
                                            error: error
                                        });
                                    },

                                    postComment: function (data, success, error) {
                                        $.ajax({
                                            type: 'post',
                                            dataType: "json",
                                            url: 'models/post_comments.php?pid=<?php echo $id;?>&cuid=<?php echo $UserId;?>&username=<?php echo $LoggedUsername;?>',
                                            data: JSON.stringify(saveComment(data)),
                                            success: function () {
                                                success(saveComment(data))
                                            },
                                            error: error
                                        });
                                    },

                                    putComment: function (data, success, error) {
                                        $.ajax({
                                            type: 'put',
                                            dataType: "json",
                                            url: 'models/put_comments.php?pid=<?php echo $id;?>&id=' + data.id,
                                            data: saveComment(data),
                                            success: function () {
                                                success(saveComment(data))
                                            },
                                            error: error
                                        });
                                    },

                                    deleteComment: function (data, success, error) {
                                        $.ajax({
                                            type: 'post',
                                            url: 'models/delete_comments.php?uid=<?php echo $UserId;?>&pid=<?php echo $id;?>&id=' + data.id,
                                            success: success,
                                            error: error
                                        });
                                    },

                                    upvoteComment: function (data, success, error) {

                                        if (data.user_has_upvoted) {
                                            $.ajax({
                                                type: 'post',
                                                dataType: "json",
                                                url: 'models/post_upvotes.php?action=post&uid=<?php echo $UserId;?>&pid=<?php echo $id;?>',
                                                data: {
                                                    comment: data.id
                                                },
                                                success: function () {
                                                    success(data)
                                                },
                                                error: error
                                            });
                                        } else {
                                            $.ajax({
                                                type: 'post',
                                                dataType: "json",
                                                url: 'models/post_upvotes.php?action=delete&uid=<?php echo $UserId;?>&pid=<?php echo $id;?>',
                                                data: {
                                                    comment: data.id
                                                },
                                                success: function () {
                                                    success(data)
                                                },
                                                error: error
                                            });
                                        }
                                    },

                                    hashtagClicked: function (hashtag) {
                                        location = 'tags-' + hashtag + '-1'
                                    },

                                    pingClicked: function(ping) {
                                        location = 'profile-' + ping + '-' + ping
                                    },

                                    timeFormatter: function (time) {
                                        moment.locale('es');
                                        return moment.utc(time, 'YYYY-MM-DD HH:mm:ss').local().fromNow();
                                    }

                                });
                            });

@Johny-rescaf
Copy link

Hello please i dont understand when the getUsers is called.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants