说明
mongoose
返回的数据并不是object
,但是通过typeof
检测他是object
.实际上是mongoose
自己封装的对象- 他会将这个对象与定义的
model
实时查询是否符合预定义
问题
var user = await UserModel.findOne({ username })
const name = user.username
console.log(name)//undefined
实际上打印出来的name
是undefined
- 我们需要使用
toObject()
方法将其转化为我们需要的对象
const userObj = user.toObject()
console.log(userObj.username)