无法读取未定义的 属性“_id”
Cannot read property '_id' of undefined
我正在尝试将此 weather API 中的文档插入到我的 MongoDB 中。我不断收到 Cannot read property '_id' of undefined
这些错误
undefined
/Users/Ekom/WebstormProjects/WebApps/node_modules/mongodb/lib/mongo_client.js:236
throw err
^
TypeError: Cannot read property '_id' of undefined
at Collection.insertMany (/Users/Ekom/WebstormProjects/WebApps/node_modules/mongodb/lib/collection.js:513:17)
at Collection.insert (/Users/Ekom/WebstormProjects/WebApps/node_modules/mongodb/lib/collection.js:825:15)
at /Users/Ekom/WebstormProjects/WebApps/app.js:26:24
at connectCallback (/Users/Ekom/WebstormProjects/WebApps/node_modules/mongodb/lib/mongo_client.js:314:5)
at /Users/Ekom/WebstormProjects/WebApps/node_modules/mongodb/lib/mongo_client.js:233:11
at _combinedTickCallback (internal/process/next_tick.js:67:7)
at process._tickCallback (internal/process/next_tick.js:98:9)
app.js
mongodb.MongoClient.connect('mongodb://localhost:27017/weatherdb', function(error, database) {
if(error != null) {
throw error;
}
collection = database.collection('data');
weather.find({search: 'Ottawa, ON', degreeType: 'C'}, function(err, documents) {
if(err) console.log(err);
// console.log(JSON.stringify(documents, null, 2));
});
console.log(documents);
collection.insert(documents, { w:1
},
function (error, result) {
if (error != null) {
console.log("ERROR: " + error);
}
});
});
请知道我做错了什么的人能给我指出正确的方向吗?
var mongodb=require("mongodb");
var weather = require('weather-js');
mongodb.MongoClient.connect('mongodb://localhost/weatherdb', function(error, database) {
if(error != null) {
throw error;
};
weather.find({search: 'Ottawa, ON', degreeType: 'C'}, function(err, documents) {
if(err) console.log(err);
console.log(documents);
database.collection('data').insert(documents,function (error, result) {
if (error) {
console.log("ERROR: " + error);
}
});
});
});
你的 console.log
行在回调函数之外,因此没有意识到 "documents" 并且你的插入语句也有点不对劲,上面的代码对我有用。
我正在尝试将此 weather API 中的文档插入到我的 MongoDB 中。我不断收到 Cannot read property '_id' of undefined
这些错误
undefined
/Users/Ekom/WebstormProjects/WebApps/node_modules/mongodb/lib/mongo_client.js:236
throw err
^
TypeError: Cannot read property '_id' of undefined
at Collection.insertMany (/Users/Ekom/WebstormProjects/WebApps/node_modules/mongodb/lib/collection.js:513:17)
at Collection.insert (/Users/Ekom/WebstormProjects/WebApps/node_modules/mongodb/lib/collection.js:825:15)
at /Users/Ekom/WebstormProjects/WebApps/app.js:26:24
at connectCallback (/Users/Ekom/WebstormProjects/WebApps/node_modules/mongodb/lib/mongo_client.js:314:5)
at /Users/Ekom/WebstormProjects/WebApps/node_modules/mongodb/lib/mongo_client.js:233:11
at _combinedTickCallback (internal/process/next_tick.js:67:7)
at process._tickCallback (internal/process/next_tick.js:98:9)
app.js
mongodb.MongoClient.connect('mongodb://localhost:27017/weatherdb', function(error, database) {
if(error != null) {
throw error;
}
collection = database.collection('data');
weather.find({search: 'Ottawa, ON', degreeType: 'C'}, function(err, documents) {
if(err) console.log(err);
// console.log(JSON.stringify(documents, null, 2));
});
console.log(documents);
collection.insert(documents, { w:1
},
function (error, result) {
if (error != null) {
console.log("ERROR: " + error);
}
});
});
请知道我做错了什么的人能给我指出正确的方向吗?
var mongodb=require("mongodb");
var weather = require('weather-js');
mongodb.MongoClient.connect('mongodb://localhost/weatherdb', function(error, database) {
if(error != null) {
throw error;
};
weather.find({search: 'Ottawa, ON', degreeType: 'C'}, function(err, documents) {
if(err) console.log(err);
console.log(documents);
database.collection('data').insert(documents,function (error, result) {
if (error) {
console.log("ERROR: " + error);
}
});
});
});
你的 console.log
行在回调函数之外,因此没有意识到 "documents" 并且你的插入语句也有点不对劲,上面的代码对我有用。