node.js - assert.equal(3, result.result.n);类型错误 : Cannot read property 'n' of undefined

标签 node.js

我收到一条错误消息,提示 n 在第 47 行没有被拒绝,我已经使用了所有方法来解决它,但我无法解决它。它正在显示并且错误是 n 是 undefiend 我想知道为什么它会给我一个错误以及我为解决它所做的真正错误是什么

const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');

// Connection URL
const url = 'mongodb://localhost:27017';

// Database Name
const dbName = 'fruitDB';

// Create a new MongoClient
const client = new MongoClient(url);

// Use connect method to connect to the Server
client.connect(function (err) {
    assert.equal(null, err);
    console.log("Connected successfully to server");

    const db = client.db(dbName);

    insertDocuments(db, function() {
        client.close();
      });
});

const insertDocuments = function(db, callback) {
    // Get the documents collection
    const collection = db.collection('fruits');
    // Insert some documents
    collection.insertMany([
        {
            name: "apple",
            score:5,
            review: "very very nice"
        },
        { 
            name: "orange",
            score: 7,
            review: "very very sour"
        },
        { 
            name: "Banana",
            score: 5,
            review: "best fruit"
        }
    ], function(err, result) {
      assert.equal(err, null);
      assert.equal(3, result.result.n);
      assert.equal(3, result.ops.length);
      console.log("Inserted 3 documents into the collection");
      callback(result);
    });
  }

请解决

最佳答案

由于result对象改变了,它不再包含result或ops字段

function (err, result)
{
    assert.equal(err, null);
    assert.equal(3, result.insertedCount);
    assert.equal(3, Object.keys(result.insertedIds).length);
    console.log("Inserted 3 documents into the collection");
    callback(result);
});

使用这些断言语句,它们应该适合您的情况。

关于node.js - assert.equal(3, result.result.n);类型错误 : Cannot read property 'n' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68497648/

相关文章:

javascript - node.js 到 imagemagick 命令中断

php - 执行 Javascript 服务器端

javascript - Sequelize 文档更新

javascript - 预过滤条件 - deep-diff Node.js

javascript - 如何缓解 Node.js 中的 Slowloris?

javascript - 错误 [ERR_HTTP_HEADERS_SENT] : Cannot set headers after they are sent to the client (multiple data entries)

javascript - JSON.parse 转换为字符串而不是传入数组

javascript - 使用 webpack 导入某些依赖项时,我得到 'Error: Cannot find module "."'

javascript - 找不到模块 express——在 Windows 上

node.js - 最小 Websocket Nodejs 尾部示例