Javascript 未执行流回调中的Mongoose findOne
当我对与周围流不同的集合执行findOne查询时,它实际上并不执行回调(执行1或2)。这是我的密码: schema.js:Javascript 未执行流回调中的Mongoose findOne,javascript,node.js,mongodb,mongoose,Javascript,Node.js,Mongodb,Mongoose,当我对与周围流不同的集合执行findOne查询时,它实际上并不执行回调(执行1或2)。这是我的密码: schema.js: 'use strict'; var mongoose = require('mongoose'), Schema = mongoose.Schema; var AutocompleteSchema = new Schema({ nGram: String, locations: [{ type: Schema.Types.ObjectId, ref: '
'use strict';
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var AutocompleteSchema = new Schema({
nGram: String,
locations: [{ type: Schema.Types.ObjectId, ref: 'Location' }]
});
module.exports = mongoose.model('Autocomplete', AutocompleteSchema);
ingest.js:
var Autocomplete = require('./schema');
var nGramAPI = require('ngram');
var cache = [];
function storeNGram(input, location) {
if(cache.indexOf(input) === -1) {
cache.push(input);
Autocomplete
.findOne({ nGram: input })
.populate('locations')
.exec(function (err, nGram) {
console.log(nGram);
if(!nGram) {
var newAutocomplete = {
nGram: input,
locations: [location._id]
};
Autocomplete.create(newAutocomplete, function(err, created) {
cache.splice(cache.indexOf(input), 1);
});
}
else {
nGram.locations.push(location._id);
sortLocations(nGram);
location.save(function(err, saved){
cache.splice(cache.indexOf(input), 1);
});
}
});
}
else {
setTimeout(function() {
storeNGram(input, location);
}, 100);
}
}
exports.ingest = function() {
console.log("Building nGrams");
var stream = Location.find().stream();
stream.on('data', function (location) {
var length = location.dispName.length > 20 ? 20 : location.dispName.length;
for(var i = 1; i <= length; i++) {
_.each(nGramAPI(i)(location.dispName), function(nGram) {
storeNGram(nGram, location);
});
}
});
}
var Autocomplete=require('./schema');
var nGramAPI=require('ngram');
var缓存=[];
功能存储RAM(输入、位置){
if(cache.indexOf(输入)=-1){
cache.push(输入);
自动完成
.findOne({nGram:input})
.populate('位置')
.exec(函数(err、nGram){
控制台日志(nGram);
如果(!nGram){
var newAutocomplete={
nGram:输入,
地点:[地点。_id]
};
创建(newAutocomplete,函数(err,created){
拼接(cache.indexOf(输入),1);
});
}
否则{
nGram.locations.push(位置标识);
分拣位置(nGram);
位置.保存(功能(错误,已保存){
拼接(cache.indexOf(输入),1);
});
}
});
}
否则{
setTimeout(函数(){
存储内存(输入、位置);
}, 100);
}
}
exports.inset=函数(){
控制台日志(“构建nGrams”);
var stream=Location.find().stream();
stream.on('data',函数(位置){
var length=location.dispName.length>20?20:location.dispName.length;
对于(var i=1;i回调是否传递了err
参数?您是否可以记录该参数并不忽略它?实际定义的模型变量Autocomplete
在哪里?您的代码只分配module.exports
,因此缺少一些内容。\u1.each(nGram(i)(location.dispName)应该是\u1.each(nGram[i][location.dispName]但我不知道那是你的problem@BrianNoah我刚刚意识到这可能与它有很大关系,我正在使用一个名为nGram的第三方库,我已经将它标记为相同的函数(nGram),今晚我将更新它,看看它是否改变了什么。这似乎没有解决它。