【问题标题】:how to fix 'Error: TypeError: Cannot read property 'catch' of undefined ' when saving a user如何修复'错误:TypeError:保存用户时无法读取未定义'的属性'catch'
【发布时间】:2019-10-19 15:26:14
【问题描述】:

有一个用户模型和app.js,

在 app.js 中,我们对 user.save() 有一个捕获,它会在控制台中引发错误:

TypeError: Cannot read property 'catch' of undefined 

用户模型:

const Schema = mongoose.Schema

const UserSchema = new Schema({
    email: {
        type: String,
        required: true,
        unique: true,
        trim: true,
        minLength: 3        
    },
    password: {
        type: String,
        required: true,
        minLength: 5
    }

})

module.exports = mongoose.model( 'users', UserSchema )

app.js 文件:

const mongoose = require( 'mongoose')
mongoose.Promise = global.Promise

const bcrypt = require( 'bcryptjs')
const User = require('./models/User')
const express = require('express')
const app = express()

const bodyParser = require('body-parser')
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))



app.listen( 4111, () => {
    console.log( 'listening on 4111 ...' )
})

mongoose.connect( 'mongodb://127.0.0.1/login', () => {
    console.log( 'Connected ! ')
})

app.post('/register', ( req, res ) => {
    let newUser = new User()
    newUser.email = req.body.email || 'gmail'
    newUser.password = req.body.password || 1234
    // res.send( newUser )
    bcrypt.genSalt( 10,  salt => {
        bcrypt.hash( req.body.password, salt, ( err, hash ) => {
            if( err ) return err 
            newUser.password = hash
            newUser.save().then( savedUser => {
                res.send( 'The user saved: ', savedUser )
             }).catch( (err) => {
                res.send( 'The user was not saved : ', err )
            })
        })
    } ).catch( err => {
        res.send( 'gen salt err :', err )
    })
})

我在这里看到了一些类似的问题,但与这个不同。

【问题讨论】:

  • 这是因为您没有从函数返回任何内容。 .catch 只能用于承诺
  • @GaneshKarewad tnx 为答案,但我为 app.js 这样做了,现在 POSTMAN 没有响应并说:正在加载......我还替换了内部然后......用 try.. .catch 但结果是相同的加载循环

标签: node.js promise model try-catch typeerror


【解决方案1】:

.catch 可以按承诺使用。因为你在这里没有返回任何承诺(或任何东西),就像在未定义上使用 .catch 一样。改用 try catch

app.post('/register', ( req, res ) => {
let newUser = new User()
newUser.email = req.body.email || 'gmail'
newUser.password = req.body.password || 1234
// res.send( newUser )
try{
  bcrypt.genSalt( 10,  salt => {
      bcrypt.hash( req.body.password, salt, ( err, hash ) => {
         if( err ) return err 
         newUser.password = hash
         newUser.save().then( savedUser => {
             res.send( 'The user saved: ', savedUser )
          }).catch( (err) => {
             res.send( 'The user was not saved : ', err )
         })
       })
  })
 } catch(err){
     res.send( 'gen salt err :', err )    
  }
})

【讨论】:

  • 我是为 app.js 这样做的,现在 POSTMAN 没有响应并说:Loading ... 我还替换了内部 then ... catch 与 try... catch 但结果相同循环。
  • 不要将内部 the.catch 替换为 try 和 catch 作为它的承诺,它应该按照已经编写的方式使用
  • Tnx 兄弟!有用!我还替换了 res.send( 'The user saved: ', savedUser ),它被 res.send(`The user saved: ${userSaved}` ) 贬低了
猜你喜欢
  • 2019-08-19
  • 2022-01-16
  • 1970-01-01
  • 2021-10-03
  • 2021-10-28
  • 1970-01-01
  • 2018-02-27
  • 2022-01-12
  • 2022-11-22
相关资源
最近更新 更多