gpt4 book ai didi

node.js - Node.js 应用程序中的类型错误

转载 作者:太空宇宙 更新时间:2023-11-04 03:20:48 24 4
gpt4 key购买 nike

我从 Node.js 教科书中复制了这个示例程序:

var MongoClient = require('mongodb').MongoClient;

var website = {
url: 'http://www.google.com',
visits: 0
};

var findKey = {
url: 'www.google.com'
}

MongoClient.connect('mongodb://127.0.0.1:27017/demo', { useNewUrlParser: true }, function(err, client) {
var db = client.db('demo');
if(err) throw err;

var collection = db.collection('websites');

collection.insert(website, function(err, docs) {

var done = 0;
function onDone(err) {
done++;
if(done < 4) return;

collection.find(findKey).toArray(function(err, results) {
console.log('Visits:', results[0].visits);

//cleanup
collection.drop(function() {
client.close();
});
});
}

var incrementVisits = {
'$inc': {
'visits': 1
}
};
collection.update(findKey, incrementVisits, onDone);
collection.update(findKey, incrementVisits, onDone);
collection.update(findKey, incrementVisits, onDone);
collection.update(findKey, incrementVisits, onDone);
});
});

当我运行它时,它抛出此错误:

/Users/me/Documents/Beginning NodeJS/node_modules/mongodb/lib/utils.js:132
throw err;
^

TypeError: Cannot read property 'visits' of undefined
at /Users/me/Documents/Beginning NodeJS/update/2update.js:26:43
at result (/Users/me/Documents/Beginning NodeJS/node_modules/mongodb/lib/utils.js:414:17)
at executeCallback (/Users/me/Documents/Beginning NodeJS/node_modules/mongodb/lib/utils.js:406:9)
at handleCallback (/Users/me/Documents/Beginning NodeJS/node_modules/mongodb/lib/utils.js:128:55)
at self.close (/Users/me/Documents/Beginning NodeJS/node_modules/mongodb/lib/cursor.js:905:60)
at handleCallback (/Users/me/Documents/Beginning NodeJS/node_modules/mongodb/lib/utils.js:128:55)
at completeClose (/Users/me/Documents/Beginning NodeJS/node_modules/mongodb/lib/cursor.js:1044:14)
at Cursor.close (/Users/me/Documents/Beginning NodeJS/node_modules/mongodb/lib/cursor.js:1057:10)
at /Users/me/Documents/Beginning NodeJS/node_modules/mongodb/lib/cursor.js:905:21
at handleCallback (/Users/me/Documents/Beginning NodeJS/node_modules/mongodb-core/lib/cursor.js:199:5)

我看不出这里出了什么问题,但教科书已经有几年了,我已经遇到过代码已经过时并且无法工作的问题,所以我想检查一下这里是否是这种情况。

最佳答案

这是您所遵循的一个非常可怕的示例,但基本上存在一些错误,这些错误本质上是由 http:///www.google.com 复合而成的,该错误是由于文档中的值与 www.google.com 不同而创建的,因此当您尝试从空数组读取属性时,您不会得到结果,并且它是未定义

基本的更正是解决这个问题,并在所有情况下实际使用 findOneAndUpdate() ,因为这将自动返回一个文档。

var MongoClient = require('mongodb').MongoClient;

var website = {
url: 'http://www.google.com',
visits: 0
};

var findKey = {
url: 'http://www.google.com'
}

MongoClient.connect('mongodb://127.0.0.1:27017/demo', { useNewUrlParser: true }, function(err, client) {
var db = client.db('demo');
if(err) throw err;

var collection = db.collection('websites');

collection.findOneAndUpdate(
findKey, website, { upsert: true },function(err, doc) {

var done = 0;

function onDone(err,doc) {
done++;

console.log("Visits: %s", doc.value.visits);
if (done >= 4) {
collection.drop(function(err) {
client.close();
});
}

}

var incrementVisits = {
'$inc': {
'visits': 1
}
};

var options = { returnOriginal: false };

collection.findOneAndUpdate(findKey, incrementVisits, options, onDone);
collection.findOneAndUpdate(findKey, incrementVisits, options, onDone);
collection.findOneAndUpdate(findKey, incrementVisits, options, onDone);
collection.findOneAndUpdate(findKey, incrementVisits, options, onDone);
});
});

请注意,最后的“四个”调用不会立即解决。这些只是将异步函数排队,并且不保证它们的执行顺序。

但是脚本将返回:

Visits: 1
Visits: 2
Visits: 3
Visits: 4

一个更好且“现代”的例子是:

const { MongoClient } = require("mongodb");

const uri = "mongodb://localhost:27017/";
const options = { useNewUrlParser: true };

const website = {
url: 'http://www.google.com',
visits: 0
};

const findKey = { url: 'http://www.google.com' };

(async function() {

try {

const client = await MongoClient.connect(uri,options);

const db = client.db('demo');
const collection = db.collection('websites');

await collection.insertOne(website);

var times = 4;

while (times--) {
let doc = await collection.findOneAndUpdate(
findKey,
{ $inc: { visits: 1 } },
{ returnOriginal: false },
);
console.log("Visits: %s", doc.value.visits);
}

await collection.drop();
client.close();

} catch(e) {
console.error(e);
} finally {
process.exit();
}

})()

由于我们实际上awaitwhile循环中执行的每个调用,我们保证这些实际上是按顺序执行的。我们还等待一切,因此代码干净且有序,当一切完成后我们可以挂起数据库连接,而不需要等待回调来解析或其他方法。

关于node.js - Node.js 应用程序中的类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50618126/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com