根据MongoDB中count()的结果分配变量值

时间:2017-12-05 20:50:16

标签: node.js mongodb count

在以下插入中,我想根据sUserRole结果为count分配值。现在,在密钥userRole下插入文档后,MongoDB中的文件为null。我做错了什么?

user.signupUser = (jUserData, fCallback) => {

        var sUserRole;

        global.db.collection('users').count((err, count) => {
            if (err) throw err;
            else if (count === 0) {
                sUserRole = "admin";
            }
            sUserRole = "user";
        })
        var jUser = {
            userName: jUserData.txtUserEmailorPhoneNumber,
            userRole: sUserRole,
            firstName: jUserData.txtUserFirstName,
            lastName: jUserData.txtUserLastName,
            password: jUserData.txtUserPassword,
            image: global.sSignupUserImagePath
        }
        global.db.collection('users').insertOne(jUser, (err, jResult) => {
            if (err) {
                var jError = {
                    "status": "error",
                    "message": "ERROR -> signupUser -> user.js -> 001"
                }
                return fCallback(false, jError)
            }
            var jOk = {
                "status": "ok",
                "message": "user.js -> user saved -> 000"
            }
            return fCallback(false, jOk)
        })
    }

0 个答案:

没有答案