运行下面的代码在检查文档状态是否已更改时,我收到意外的标识符错误。我用Google搜索了这个死讯。试图在Mongodb,这里和其他misc源找到文档,没有任何运气。我花了整整一天的时间,现在感觉非常愚蠢。
真诚感谢所有帮助。
var MongoClient = require('mongodb').MongoClient;
MongoClient.connect('mongodb://localhost:27017/course', function(err, db) {
if (err) throw err;
var weather = db.collection('weather');
var filter = {};
var projection ={'State':1, 'Temperature':1, _id:true};
var options = { 'skip' : 0,
'limit' : 20000 ,
'sort' : [['State', 1], ['Temperature', -1]] };
var cursor = weather.find(filter, projection, options);
var month_high = false;
var prevState = '';
var curID = '';
var operation = {'_id':curID,'$set':{'month_high':true}};
// get first document
cursor.first();
// initialize vars
prevState = doc.State;
// cycle through documents
cursor.each(function(err, doc) {
if(err) throw err;
if(doc == null) {
return db.close();
}
//Save current document Id
curID = doc._id;
// Check if State has changgd
if prevState != doc.state{
//If State has changed update the document
weather.update(operation,function(err,doc));
console.dir(doc); //we expect four documents to ouput.
}
// save current doc.state as previous state
prevState = doc.State;
;})
});
});
答案 0 :(得分:1)
我认为谷歌很难找到意想不到的标识符。您可能有额外的结束括号或右括号。
我没有看到这个开头括号的右括号:
MongoClient.connect(
答案 1 :(得分:0)
将if prevState != doc.state{
更改为if (prevState != doc.state) {
- 即:添加一对(
)
。
要轻松找到此类问题,您应该使用 Javascript语法检查程序。谷歌这个词,你会得到一些好的点击。
另请注意,您的代码中至少还有一个其他语法问题
weather.update(operation,function(err,doc));
您使用function
关键字但未提供功能正文。要使其通过语法检查,您至少应该:weather.update(operation,function(err,doc) {});
但是你可能想在花括号{}
中添加一些逻辑。
这是http://esprima.org/demo/validate.html
检测到的还有一件事与原始问题无关:
您在代码中使用throw
来报告错误。由于node / mongo主要使用async API抛出异常几乎毫无意义。看一下如下问题: