Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.8k views
in Technique[技术] by (71.8m points)

node.js - Can we define the two different tasks independently without promises, callbacks or async/await?

Here I want to know the difference between the two code layouts execution?

How will they execute or will the execution be the same?

router.route('/addTasks-writers').get(function (req, res) {

    User.findOneAndUpdate({ _id: req.query.val }, { $push: { Addtasks: element } }, function (error, data) {
        if (data) {
            console.log("data pushed to the Addtasks in Writer");

            User.updateMany({ "Addtasks._id": req.query.id },
                {
                    $set: {
                        "Addtasks.$.assignee": req.query.assignee,
                        "Addtasks.$.deadline": req.query.deadline,
                        "Addtasks.$.status": 'Requirement Submitted',
                        "Addtasks.$.background": 'linear-gradient(223.88deg, #FF149D 8.89%, #620F32 94.31%)'
                    }
                },
                function (error, success) {
                    if (error) {
                        console.log("error");
                        res.end('{"msg" : "Unable to set the Writer and deadline for the Task", "status" : 700}');
                    } else {
                        console.log("req.query.assignee = " + req.query.assignee);
                        console.log("req.query.assignee = " + req.query.deadline);
                        console.log("updated the assignee in admin dashboard");
                        res.end('{"success" : "Updated Successfully", "status" : 200}');
                    }
                })
        }
    })
}

And this one here below both the mongoose queries are independently placed. They are not nested one inside another.

router.route('/addTasks-writers').get(function (req, res) {
    User.findOneAndUpdate({ _id: req.query.val }, { $push: { Addtasks: element } }, function (error, data) {
        if (data) {
            console.log("data pushed to the Addtasks in Writer");
        }
        else {
            console.log("error = " + error);
        }
    });

    User.updateMany({ "Addtasks._id": req.query.id },
        {
            $set: {
                "Addtasks.$.assignee": req.query.assignee,
                "Addtasks.$.deadline": req.query.deadline,
                "Addtasks.$.status": 'Requirement Submitted',
                "Addtasks.$.background": 'linear-gradient(223.88deg, #FF149D 8.89%, #620F32 94.31%)'
            }
        },
        function (error, success) {
            if (error) {
                console.log("error");
                res.end('{"msg" : "Unable to set the Writer and deadline for the Task", "status" : 700}');
            } else {
                console.log("req.query.assignee = " + req.query.assignee);
                console.log("req.query.assignee = " + req.query.deadline);
                console.log("updated the assignee in admin dashboard");
                res.end('{"success" : "Updated Successfully", "status" : 200}');
            }
        })
})

P.S. Please tell me if it's a bad practice to write the code like this callback hells. What can be done to improve this? Is nested callbacks will get errors?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...