Node.js Return语句不起作用,但console.log()起作用

时间:2019-11-10 08:31:30

标签: javascript node.js express pg

我能够从数据库中获取数据,但返回该数据作为对所发出请求的响应是一个问题。

 `    // create employees and save into the databse
async save(resp) {

    this.bcrypt.hash(this.user.password, 10).then( (hash)=>{

        let params = [this.user.firstName,
            this.user.lastName,this.user.email, 
            this.user.password, this.user.gender, 
            this.user.jobRole,this.user.department, 
            this.user.address];

       this.client.query( 
        'INSERT into employees (firstName, lastName, email, password, gender, jobRole, department, address) VALUES($1, $2, $3, $4, $5, $6, $7, $8) RETURNING userId', 
         params)

        .then ( (result) => {

            try {
                let userFetched = result.rows[0].userid;
                this.client.query("SELECT * FROM employees WHERE userid = $1", [userFetched])
                    .then( (res)=>{

                        console.log(res.rows[0]);
                        return res.rows[0];

                    })

                    .catch( (err)=>{
                        console.log(err);
                    })


            } catch (error) {
                return {
                    error: "Employee could not be created correctly."
                }
            }

        })

        .catch( (err)=>{
            return {
                error: "Something went wrong. Try again later."
            }
        })


    })

}`

如果我对获取的数据进行控制台,那是可行的,但是返回它是问题所在。我不断收到错误消息。请帮忙。

0 个答案:

没有答案