我收到一条错误消息,提示 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/