【发布时间】:2018-03-07 10:29:18
【问题描述】:
我想查询一个集合并使用我将从另一个查询中获得的某个值更新每个文档,该查询将使用返回文档中的一些信息构建。
const mongoose = require('mongoose');
const userModel = {
country: { type: String }
newField: { type: String }
};
const myUsersModel = mongoose.model('user',userModel);
myUsersModel.find({country:"USA"}).forEach(function (doc) {
// another query here into a relation Database:
let anotherQuery = 'SELECT * FROM myTable WHERE name=' + doc.name;
mySQLConnection.query(
anotherQuery,
function selectCb(err, results, fields) {
if (err) {
console.log("ERROR: " + err.message);
throw err;
}
console.log("Got "+results.length+" Rows:");
let updatedInfo = results.SomeField;
// update the mongoose doc:
doc.newField = updatedInfo;
myUsersModel.save(doc);
});
mySQLConnection.end(function(err) {
console.log("connection ended.");
});
mongoose.connection.close();
});
我收到以下错误:
TypeError: myUsersModel.find(...).forEach 不是函数
【问题讨论】:
标签: javascript sql node.js mongodb mongoose