据我所知,我已正确设置了此调用的承诺,并且res.send();
似乎确实被调用了,但是有一个错误被抛出而且所有它告诉我的是错误是未定义的。这对我来说显然没什么帮助。
似乎导致问题的一行是:
db.user.findOne({ where: {username: testUser} })
.then((username) => {
res.send({'created user':username.id});
})
我正在尝试创建一个用户,然后一旦完成,请查找新创建的用户的id并为他们创建会话。用户肯定是被创建的,但是我注意到在检查chrome时传入用户名AFTER fineOne的变量是空的。
非常感谢任何帮助。
const express = require('express');
const router = express.Router();
const util = require('util');
const bcrypt = require('bcrypt');
const session = require('express-session');
const { check, validationResult } = require('express-validator/check');
//require models
const db = require("../models/index");
var input = req.body;
try{ //validation
req.check('firstName').isLength({min:1, max:100});
req.check('lastName').isLength({min:1, max:100});
req.check('email').isEmail();
req.check('username').isLength({min:1, max:15}).isAlphanumeric();
req.check('password').isLength({min:8, max:100});
// check for errors in validation obj!
var errors = req.validationErrors();
if (errors) {
res.send('There have been validation errors: ' + util.inspect(errors), 400);
return;
}
var testName = input.firstName;
var testName2 = input.lastName;
var testEmail = input.email;
var testUser = input.username;
//hash password
const saltRounds = 10;
bcrypt.hash(input.password, saltRounds, function(err, hash) {
var testPass = hash;
debugger;
//insert user and newly hashed password into db
db.user.create({
first_name:testName,
last_name:testName2,
email:testEmail,
username:testUser,
hashed_password:testPass})
.then(
// search for just added user
db.user.findOne({ where: {'username': testUser} })
.then((username) => {
res.send({'created user':username.id});
})
.catch(err=>console.log('err',err))
)
.catch(err=>console.log('error:', err));
});
}catch(error){
//console.log('validation error:',error);
}