0
圍繞異步環繞我的頭。這會在異步環境中工作嗎?會在nodejs中異步工作嗎?
發佈並檢查一個參數,並在每次發佈時返回true或false,還是會阻止?
用戶json是mongodb請求的替代者。
'use strict';
const serverport = process.env.PORT;
let users = [{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"email": "[email protected]",
"address": {
"street": "Kulas Light",
"suite": "Apt. 556",
"city": "Gwenborough",
"zipcode": "92998-3874",
"geo": {
"lat": "-37.3159",
"lng": "81.1496"
}
},
"phone": "1-770-736-8031 x56442",
"website": "hildegard.org",
"company": {
"name": "Romaguera-Crona",
"catchPhrase": "Multi-layered client-server neural-net",
"bs": "harness real-time e-markets"
}
}]
let checkName = (obj) => {
let va = users[0].email;
return (va === obj) ? true : false;
}
let express = require("express"),
app = express();
app.post('/reg', (req, res, next) => {
if (checkName(req.param.email) == true) {
//render email already taken
} else {
//render email is not taken and complete registration
}
});
app.listen(serverport, (req, res, next) => {
console.log('server is listening on port: ' + serverport);
});
當然這會奏效。什麼把你扔掉了?函數'checkName'不是沒有回調的事實?如果是這樣,那並不重要,你可以在任何地方都有你的功能。另外,如果我可能會建議好的做法:對於任何不會改變的變量(如'checkName'和'express'),使用'const'而不是'let'。另外考慮簡化你的返回值:'return va === obj' –