【问题标题】:How to fix the Type Error: Cannot read properties of undefined (reading '0')?如何修复类型错误:无法读取未定义的属性(读取“0”)?
【发布时间】:2022-01-06 23:34:29
【问题描述】:

这是我的功能。 解析时显示错误(data.ops[0])

var db = require("../config/connection")
var collection = require("../config/collection")
const bcrypt = require('bcrypt')
module.exports={
    doSignup:(userData)=>{
        return new Promise(async(resolve,reject)=>{
            userData.Password =await bcrypt.hash(userData.Password,10)
           db.get().collection(collection.USER_COLLECTION).insertOne(userData).then((data)=>{
               resolve(data.ops[0])
           })
        })
    },
}
     

【问题讨论】:

标签: node.js mongodb


【解决方案1】:

你喜欢哪个版本的 Mongodb?

因为新版本没有名为 ops 的属性,这就是为什么 resolve(data.ops[0]) 会给出您所看到的错误。

查看下面的文档会返回InsertOneResult

https://mongodb.github.io/node-mongodb-native/4.1/classes/Collection.html#insertOne

insertOne(doc: OptionalId<TSchema>): Promise<InsertOneResult<TSchema>>

如果你看到同一个https://mongodb.github.io/node-mongodb-native/4.1/interfaces/InsertOneResult.html的文档

它返回acknowledgedinsertedid。因此下面的作品

resolve(data.insertedId)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-16
    • 2021-10-01
    • 2019-08-29
    • 2023-01-08
    • 2023-02-26
    • 2018-04-29
    • 2022-12-08
    • 2022-12-01
    相关资源
    最近更新 更多