陷入Nodejs的循环嵌套--大坑

923 阅读3分钟

最近两周都在学nodejs,虽然之前有自学过,但是发现自学还是有一定缺陷的,所学的知识只是零碎。今天就循环嵌套这个话题进行如下总结:
话题背景是这样的。基于一个查询好友列表的实现,我需要在postman测试显示出我的好友,特别关心,黑名单三类好友,如下图:

当然我的里面只有好友,特别关心、黑名单均为空。
需要的前提,创建好自己的数据库,写好路由框架。在此,我建立的好友列表和一个用户列表。

在此,我就要说说我当时是怎么入坑了。因为我的好友列表里面是双向好友,user_Id和fri_Id都可能出现我自己的账号。

那么我就想了:先判断user_Id和fri_Id两个是否存在我的Id,存在再在返回的结果里面分两种情况遍历。一种user_Id有我的Id,把fri_Id与用户基本信息表相连,查询带回。想法很完美,但是实现非常艰难,在执行过程中for循环与查询根本就是异步的,for执行带回的值就是undefined,根本无法实现在for里面实现同步操作。我也百度过,使用ES6的async,step,Promise。对ES6不怎么熟悉的我,用了step试了一下,网上我也不知道是因为写的函数简单还是啥,我照百度上的写了一个step,终端运行给我报stepundefined...emmm,我step安装了,还是给我报...Emmm。之后我就果断放弃了。因为我还发现了另外一个大bug,res.json不能返回多次,也就是常见的一次请求多次响应报错。我这种小白实在是扛不住啊。
之后吧,我就又想了一个方法,也就是现在完成的方法。写个存储过程,设置两个传入的参数,分别为用户Id和好友分类,写三个方法分别获取存储过程的三个好友分类的好友信息。
思路是这样的。通过判断我的账号里面有没有好友,有好友就获取三个分类的好友信息,没有就返回。获取的时候,我才用的又是循环嵌套,emmm,勉强完成了。但是我觉得它的效率并不是很高,明天开始学ES6,致力搞懂异步和同步吧。 下面是应用层和数据层的代码(相应的包需要你自己导入哦):

        var userId = req.user[0].base_info_Id
            // console.log(req.user)
        console.log('我的:' + userId)
        messageDAO.friendList(userId, function(err, results) {
            if (err) {
                res.json({ code: 500, msg: '好友列表查询失败!' })
            } else {
                // console.log(results)
                if (results.length > 0) {
                    messageDAO.friends(userId, function(err, results1) {
                        if (err) {
                            res.json({ code: 500, msg: '好友列表查询失败!' })
                        } else {
                            console.log('好友')
                            console.log(results1[0])
                            messageDAO.attFriends(userId, function(err, results2) {
                                if (err) {
                                    res.json({ code: 500, msg: '好友列表查询失败!' })
                                } else {
                                    console.log('关注')
                                    console.log(results2[0])
                                    results1[1] = results2[0]
                                    console.log(results1)
                                    messageDAO.blackFriends(userId, function(err, results3) {
                                        if (err) {
                                            res.json({ code: 500, msg: '好友列表查询失败!' })
                                        } else {
                                            console.log('黑名单')
                                            console.log(results3[0])
                                            results1[2] = results2[0]
                                            res.json({ code: 200, data: results1, msg: '好友列表查询成功!' })

                                        }
                                    })
                                }
                            })
                        }
                    })
                } else {
                    res.json({ code: 200, data: results, msg: '好友列表无好友!' })
                }
            }


        })
    }
friendList: function(userId, callback) {
        DAO('select fri_Id,user_Id from friends where (user_Id = 20003 or fri_Id = 20003)and fri_status = 1', [userId, userId], function(err, results) {
            // console.log(results)
            if (err) {
                callback(err, null)
            } else {
                callback(null, results)
            }
        })
    },
    friends: function(userId, callback) {
        DAO('call pro_friends(?,0);', [userId], function(err, results) {
            // console.log(results)
            if (err) {
                callback(err, null)
            } else {
                callback(null, results)
            }
        })
    },
    attFriends: function(userId, callback) {
        DAO('call pro_friends(?,1);', [userId], function(err, results) {
            // console.log(results)
            if (err) {
                callback(err, null)
            } else {
                callback(null, results)
            }
        })
    },
    blackFriends: function(userId, callback) {
        DAO('call pro_friends(?,2);', [userId], function(err, results) {
            // console.log(results)
            if (err) {
                callback(err, null)
            } else {
                callback(null, results)
            }
        })
    }

希望这个能帮助在初学nodejs且还没有学ES6的童鞋,避免像我一样陷入循环嵌套的大坑。