在保存作者之前,我试图检查作者是否在 mongo 中。当我通过创建作者功能运行我的第一个 rss 文档时,所有作者都保存在数据库中 - 即使他们在提要中创作了两个项目。
奇怪的是,如果我再次运行提要, Mongoose 似乎知道作者已经存在并且不会再次添加他们。有人可以向我解释发生了什么吗?
function insertFeedItems(feedItems, newFeedID, callback) {
feedItems.forEach((item) => {
let firstName = item.author.substr(0, item.author.indexOf(' '));
let lastName = item.author.substr(item.author.indexOf(' ') + 1);
authorController.createAuthorInternally(firstName, lastName, function(author) {
let aid = author.id;
categoryController.createCategoryInternally(item.categories, function(categories) {
// console.log('author: '+aid);
// console.log('categories: '+categories);
});
});
});
}
exports.createAuthorInternally = (firstName, lastName, callback) => {
let author = authorFilter.validateAuthor(firstName, lastName);
let queryParams = {
$and: [{firstName: {$regex: firstName, $options: 'i'}},
{lastName: {$regex: lastName, $options: 'i'}}],
};
let query = Author.findOne(queryParams).sort([['lastName', 'ascending']]);
let findAuthor = query.exec();
findAuthor.then((foundAuthor)=> {
if (foundAuthor === null) {
f1();
}
});
function saveGuy() {
return new Promise((resolve) => {
let insertNewAuthor = author.save();
resolve(insertNewAuthor);
});
}
async function f1() {
var name = await saveGuy();
}
};
编辑:我用不同的方式尝试过:
Author.count(({'firstName': firstName}, { 'lastName': lastName }), function (err,count) {
console.log(firstName + " " + lastName + " " + count);
if(count === 0){
f1();
}
});
function saveGuy() {
return new Promise((resolve) => {
let insertNewAuthor = author.save();
resolve(insertNewAuthor);
});
}
async function f1() {
var name = await saveGuy();
}
第一次运行这个新方法时,输出是:
Jon Brodkin 0
Peter Bright 0
Timothy B. Lee 0
Samuel Axon 0
Kyle Orland 0
Jon Brodkin 0
Kyle Orland 0
Megan Geuss 0
Cyrus Farivar 0
Peter Bright 0
Jim Resnick 0
Cyrus Farivar 0
Kyle Orland 0
Beth Mole 0
Ars Staff 0
Megan Geuss 0
Cyrus Farivar 0
John Timmer 0
Kyle Orland 0
Samuel Axon 0
第二次运行相同的 rss 提要时:
Cyrus Farivar 3
Jon Brodkin 2
Kyle Orland 4
Megan Geuss 2
Jon Brodkin 2
Ars Staff 1
Peter Bright 2
Jim Resnick 1
Peter Bright 2
Kyle Orland 4
Megan Geuss 2
Cyrus Farivar 3
Timothy B. Lee 1
Samuel Axon 2
Kyle Orland 4
Beth Mole 1
Cyrus Farivar 3
John Timmer 1
Kyle Orland 4
Samuel Axon 2
关于提供赏金,这是我用来查找和保存的方法:
exports.createAuthorInternally = (firstName, lastName, callback) => {
let query = Author.findOne({firstName: firstName,lastName: lastName});
query.exec(function(err,doc) {
if(err){console.log(err)}
if(!doc){
let auth = new Author({firstName: firstName, lastName: lastName});
auth.save(auth, function(err,newdoc) {
if(err){console.log(err)}
callback(newdoc);
});
}else{
callback(doc);
}
});
结果与前面的方法相同。
编辑: JohnnyHK 指出了我的错误。我调整了代码以反射(reflect)他的回答:
function insertFeedItems(feedItems,newFeedID){
async.eachSeries(feedItems, function(item, eachCallBack) {
let firstName = item.author.substr(0, item.author.indexOf(' '));
let lastName = item.author.substr(item.author.indexOf(' ') + 1);
async.waterfall([
(callback) => {
authorController.createAuthorInternally(firstName, lastName, function(author) {
return callback(null, author, item.categories);
});
},
(author, categories, callback) => {
categoryController.createCategoryInternally(categories, function(categories) {
return callback(null, author, categories);
});
},
(author, categories, callback) => {
feedEntryController.createFeedEntry(item, author, categories, function(entry) {
return callback(null, author, categories, entry);
});
},
],
function(waterfallError) {
if(!waterfallError){
eachCallBack();
}
});
}, function(eachSeriesErr) {
if(eachSeriesErr) {
console.log('An item failed to process');
} else {
console.log('All items have been processed successfully');
}
});
}
最佳答案
JohnnyHK 指出了我的错误。使用库 Async - 我调整了代码以反射(reflect)他的回答:
function insertFeedItems(feedItems,newFeedID){
async.eachSeries(feedItems, function(item, eachCallBack) {
let firstName = item.author.substr(0, item.author.indexOf(' '));
let lastName = item.author.substr(item.author.indexOf(' ') + 1);
async.waterfall([
(callback) => {
authorController.createAuthorInternally(firstName, lastName, function(author) {
return callback(null, author, item.categories);
});
},
(author, categories, callback) => {
categoryController.createCategoryInternally(categories, function(categories) {
return callback(null, author, categories);
});
},
(author, categories, callback) => {
feedEntryController.createFeedEntry(item, author, categories, function(entry) {
return callback(null, author, categories, entry);
});
},
],
function(waterfallError) {
if(!waterfallError){
eachCallBack();
}
});
}, function(eachSeriesErr) {
if(eachSeriesErr) {
console.log('An item failed to process');
} else {
console.log('All items have been processed successfully');
}
});
}
关于javascript - Mongoose 正在保存文档,即使我检查文档是否已经存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49639236/
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
这个问题在这里已经有了答案:Checktoseeifanarrayisalreadysorted?(8个答案)关闭9年前。我只是想知道是否有办法检查数组是否在增加?这是我的解决方案,但我正在寻找更漂亮的方法:n=-1@arr.flatten.each{|e|returnfalseife
我不确定传递给方法的对象的类型是否正确。我可能会将一个字符串传递给一个只能处理整数的函数。某种运行时保证怎么样?我看不到比以下更好的选择:defsomeFixNumMangler(input)raise"wrongtype:integerrequired"unlessinput.class==FixNumother_stuffend有更好的选择吗? 最佳答案 使用Kernel#Integer在使用之前转换输入的方法。当无法以任何合理的方式将输入转换为整数时,它将引发ArgumentError。defmy_method(number)
我有一个包含多个键的散列和一个字符串,该字符串不包含散列中的任何键或包含一个键。h={"k1"=>"v1","k2"=>"v2","k3"=>"v3"}s="thisisanexamplestringthatmightoccurwithakeysomewhereinthestringk1(withspecialcharacterslike(^&*$#@!^&&*))"检查s是否包含h中的任何键的最佳方法是什么,如果包含,则返回它包含的键的值?例如,对于上面的h和s的例子,输出应该是v1。编辑:只有字符串是用户定义的。哈希将始终相同。 最佳答案
我已经构建了一些serverspec代码来在多个主机上运行一组测试。问题是当任何测试失败时,测试会在当前主机停止。即使测试失败,我也希望它继续在所有主机上运行。Rakefile:namespace:specdotask:all=>hosts.map{|h|'spec:'+h.split('.')[0]}hosts.eachdo|host|begindesc"Runserverspecto#{host}"RSpec::Core::RakeTask.new(host)do|t|ENV['TARGET_HOST']=hostt.pattern="spec/cfengine3/*_spec.r
我需要检查DateTime是否采用有效的ISO8601格式。喜欢:#iso8601?我检查了ruby是否有特定方法,但没有找到。目前我正在使用date.iso8601==date来检查这个。有什么好的方法吗?编辑解释我的环境,并改变问题的范围。因此,我的项目将使用jsapiFullCalendar,这就是我需要iso8601字符串格式的原因。我想知道更好或正确的方法是什么,以正确的格式将日期保存在数据库中,或者让ActiveRecord完成它们的工作并在我需要时间信息时对其进行操作。 最佳答案 我不太明白你的问题。我假设您想检查
我的日期格式如下:"%d-%m-%Y"(例如,今天的日期为07-09-2015),我想看看是不是在过去的七天内。谁能推荐一种方法? 最佳答案 你可以这样做:require"date"Date.today-7 关于ruby-检查日期是否在过去7天内,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/32438063/
如何检查Ruby文件是否是通过“require”或“load”导入的,而不是简单地从命令行执行的?例如:foo.rb的内容:puts"Hello"bar.rb的内容require'foo'输出:$./foo.rbHello$./bar.rbHello基本上,我想调用bar.rb以不执行puts调用。 最佳答案 将foo.rb改为:if__FILE__==$0puts"Hello"end检查__FILE__-当前ruby文件的名称-与$0-正在运行的脚本的名称。 关于ruby-检查是否
我有一个div,它根据表单是否正确提交而改变。我想知道是否可以检查类的特定元素?开始元素看起来像这样。如果输入不正确,添加错误类。 最佳答案 试试这个:browser.div(:id=>"myerrortest").class_name更多信息:http://watir.github.com/watir-webdriver/doc/Watir/HTMLElement.html#class_name-instance_method另一种选择是只查看具有您期望的类的div是否存在browser.div((:id=>"myerrortes
我的模型有defself.empty_building//stuffend我怎样才能对这个现有的进行rspec?,已经尝试过:describe"empty_building"dosubject{Building.new}it{shouldrespond_to:empty_building}endbutgetting:Failure/Error:it{shouldrespond_to:empty_building}expected#torespondto:empty_building 最佳答案 你有一个类方法self.empty_bu