devs我是JavaScript的新手,获取增量用户ID时遇到问题。请帮助我。
我将返回一个已解析的Promise数组,但是用户ID无法自动递增;它在复制自己。
所有用户的模型
export default [];
管理员模型
import hasher from '../helpers/password';
import CONFIG from '../config/config'
import tokenMan from '../helpers/tokenMan';
const id = 1 ;
const is_admin = true;
const is_buyer = false;
const is_seller = false;
const email = 'john@gmail.com';
const token = tokenMan.tokenizer({id ,is_admin, is_seller, is_buyer});
module.exports = {
async makeAdmin(){
const hash= await hasher.hashingPassword(CONFIG.adminPwd, 10);
const admin = {
token: token,
id: id,
first_name: 'john',
last_name: 'doe',
email: email,
address: 'Lagos',
password: hash,
is_admin: is_admin,
is_seller: is_seller,
is_buyer: is_buyer
}
return admin;
}
}
用于注册和登录的用户控制器
import users from '../models/userModel';
import admin from '../models/adminModel';
const User = {
// User Sign Up
async userSignUp(req, res) {
const {
error
} = signUpFields(req.body);
if (error) return res.status(400).json({
status: 400,
error: error.details[0].message
});
const id = users.length +1;
const is_admin = false;
const {
first_name,
last_name,
email,
password,
address,
is_seller,
is_buyer
} = req.body
const hashed_password = await hasher.hashingPassword(password, 10);
try {
const newUser = {
token: tokenMan.tokenizer({id, is_admin, is_seller, is_buyer}),
id: id,
email: email,
first_name: first_name,
last_name: last_name,
password: hashed_password,
address: address,
is_admin: is_admin,
is_buyer: Boolean(is_buyer == "true"),
is_seller: Boolean(is_seller == "true"),
}
users.push(await admin.makeAdmin(), newUser);
Promise.all(users).then(values =>{
return res.status(201).json({
status: 201,
message: 'Successfully Signed Up',
data: values
})
}).catch(err =>{
throw err.message;
});
} catch (err) {
return res.status(500).json({
status: 500,
error: err.message
})
}
}
}
返回的回复正文
[ {
id: 1,
first_name: 'john',
last_name: 'doe',
email: 'john@gmail.com',
...
{
id: 1,
email: 'yachim@gmail.com',
first_name: 'yachim',
last_name: 'zed',
我想获得一个适当的增量用户ID。预先谢谢你。