插入 mongo db 后未获取 ops 数组

编程入门 行业动态 更新时间:2024-10-05 21:19:26

插入 mongo db 后未获取 ops <a href=https://www.elefans.com/category/jswz/34/1771288.html style=数组"/>

插入 mongo db 后未获取 ops 数组

当我尝试使用函数插入到 mongo db 后移动图像时

var db = require('../config/dbconnect')
module.exports = {

    //addProduct:(product,callback)=>
    addProduct: (product, callback) => {
        //console.log(product)
        db.get().collection('product').insertOne(product).then((data) => {
            console.log(data)
            callback(data.ops[0]._id)
        })
    }
}

我收到这样的错误:

{
  acknowledged: true,
  insertedId: new ObjectId("61054e43cffb994774d37ab0")
}
(node:6964) UnhandledPromiseRejectionWarning: TypeError: Cannot read property '0' of undefined
    at E:\Nodejs\ecommerce\helpdesk\product_help.js:11:27
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:6964) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see .html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:6964) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
回答如下:

这是错误的第一次回调。尝试作为结果的第二个参数。

 db.get().collection('product').insertOne(product).then((error,data)=>
       {
         console.log(data)
         callback(data.ops[0]._id)
       })

更多推荐

插入 mongo db 后未获取 ops 数组

本文发布于:2024-05-30 22:49:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1771009.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   mongo   db   ops

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!