当涉及到服务器端的东西时,我对 Node.js 或 Javascript 非常陌生。目前我正在尝试验证一些用户输入并在出现问题时设置默认值。现在,如果我运行验证,则在验证完成之前,json 对象会出现在数据库中。
- 我进行验证的方式可能不是现在最好的,但如果有人可以向我解释这种行为,我很确定我将来可以更好地理解 Javascript。
- 是否有更好的方法通过回调、中间件进行验证(无需 Mongoose 或其他 ODM 模块),或者我应该使用一些异步模块?
这是我的代码:
module.exports = function(app, express, todoDB, listDB, statusDB) {
var moment = require('moment');
var todoRouter = express.Router();
todoRouter.post('/', function(req, res, next) {
console.log('1');
if (!(moment(req.body.createDate).isValid())) {
req.body.createDate = moment().format("DD-MM-YYYY HH:mm:ss");
}
else {
req.body.createDate = moment(req.body.createDate).format("DD-MM-YYYY HH:mm:ss");
}
console.log('2');
if (req.body.list_id == '') {
listDB.findOne({list: 'Neu'}, function(reqdb, docs) {
if (docs == null) {
listDB.insert({list: 'Neu', index: 1});
listDB.findOne({list: 'Neu'}, function(reqdb, docs) {
console.log('AnlageListID');
console.log(docs._id);
req.body.list_id = docs._id;
});
}
else {
console.log('BestehendeListID');
console.log(docs._id);
req.body.list_id = docs._id;
}
});
}
console.log('3');
if (req.body.status_id == '') {
statusDB.findOne({status: 'offen'}, function(reqdb, docs) {
if (docs == null) {
statusDB.insert({status: 'offen', index: 1});
statusDB.findOne({status: 'offen'}, function(reqdb, docs) {
console.log('AnlageStatusID');
console.log(docs._id);
req.body.status_id = docs._id;
});
}
else {
console.log('BestehendeStatusID');
console.log(docs._id)
req.body.status_id = docs._id;
}
});
}
console.log('4');
console.log('StatusID');
console.log(req.body.status_id);
console.log('ListID');
console.log(req.body.list_id);
todoDB.insert({
todo: req.body.todo,
createDate: req.body.createDate,
endDate: req.body.endDate,
discription: req.body.discription,
comment: req.body.comment,
list_id: req.body.list_id,
priority_id: req.body.priority_id,
section_id: req.body.section_id,
user_id: req.body.user_id,
status_id: req.body.status_id,
company_id: req.body.company_id
});
res.json({message: 'TODO erfolgreich hinzugefügt!'});
});
return todoRouter;
};
...这是输出:
1
2
3
4
StatusID
ListID
POST /api/todos 200 76.136 ms - 44
BestehendeListID
M3Xh46VjVjaTFoCM
BestehendeStatusID
48v80B4fbO87c8um
PS:这是一个小“项目”,只是为了我学习 MEAN Stack,所以我使用 neDB。
最佳答案
如果我理解正确,您会尝试按顺序执行多个异步调用,并在代码中引入检查以验证先前的异步调用是否已完成。这在一般情况下不起作用,因为您的检查可能会在异步调用完成之前得到处理。它可能偶尔会起作用,只是偶然,但我什至没想到会这样。
有相应的标准机制。其中一种是使用 Promise,另一种是使用 async
,还有一种是将所有回调堆叠在一起。下面我将演示如何使用async
解决该问题,但相同的一般思想也适用于使用 Promise。检查 Github 上的 async
项目,下面的部分解决方案就会变得清晰:
var async = require("async")
async.waterfall([
function(next) {
listDB.findOne({list: 'Neu'}, next); // quits on error
},
function(doc, next) {
if (doc) {
return next(null, doc._id);
}
statusDB.insert({status: 'offen', index: 1}, function(err) {
if (err) return next(err); // quit on error
statusDB.findOne({status: 'offen'}, function(err, doc) {
next(err, doc._id); // quits on error
});
});
},
function(id, next) {
// do next step and so on
next();
}
],
// this is the exit function: it will get called whenever an error
// is passed to any of the `next` callbacks or when the last
// function in the waterfall series calls its `next` callback (with
// or without an error)
function(err) {
console.error("Error processing:", err)
});
关于node.js - 在插入数据库之前等待验证(服务器端)完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33329288/