Tag:

将promisevariables分解成命名variables

我有这样的事情 Promise.all( categories.map(category => Collection.find({ category })) ).then(items => { return items }) 但后来我只是得到一个数组,其中每个元素是在特定类别中的Collection中find的项目的数组。 我想要的是返回一个对象,其中的键是类别。 所以如果我的类别是football , volleyball和motorsport ,我想要 { 'football': […], 'volleyball': […], 'motorsport': […] } 代替 [ […], […], […] ] 就像我现在一样。 如果类别的数量是静态的,我想我可以做这样的事情 Promise.all( categories.map(category => Collection.find({ category })) ).then(([football, volleyball, motorsport]) => { return { football, volleyball, motorsport } })

使用mongoose承诺的麻烦

有人能告诉我为什么这个承诺设置不起作用。 它应该删除文档,然后添加它然后find它然后控制它。 这不是安慰数据。 var Comp = require("./models/company.js"); var arr = [ {name : "comp1",industry : "industry1", ranking: 20}, {name : "comp2",industry : "industry2", ranking: 5}, {name : "comp3",industry : "industry3", ranking: 10} ] var output = {}; var promise = Comp.find({}).exec() promise.then(function(res){ console.log("removed") return Comp.remove({}).exec() }) .then(function(){ return Comp.create(arr).exec() }) .then(function(data){ return Comp.find({}).exec(); }) .then(function(data){ […]

Node UnhandledPromiseRejectionWarning

我有一个像这个问题,我的NPM包问题,但我不知道如何解决 什么是未处理的承诺拒绝 这是我的index.js: 'use strict'; var got = require('got'); var registryUrl = require('registry-url'); var Promise = require('pinkie-promise'); module.exports = function (name) { if (typeof name !== 'string') { return Promise.reject(new Error('username required')); } return got.head(registryUrl + name.toLowerCase()) .then(function (res) { return 'test'; }) .catch(function (err) { if (err.statusCode === 404) { err.message = 'Package doesn\'t […]

用Promise.all避免唯一的错误E11000

我一直在使用这个mongoose插件来执行代码库中经常使用的findOrCreate 。 我最近意识到,在创build唯一索引时执行多个asynchronousfindOrCreate操作很容易导致E11000重复键错误。 一个例子可以用Promise.all来描述。 假设name是唯一的,那么: const promises = await Promise.all([ Pokemon.findOrCreate({ name: 'Pikachu' }), Pokemon.findOrCreate({ name: 'Pikachu' }), Pokemon.findOrCreate({ name: 'Pikachu' }) ]); 以上将肯定失败,因为findOrCreate不是primefaces的。 这是有道理的,考虑之后为什么会失败,但我想要的是一个简化的方法来解决这个问题。 我的许多模型使用findOrCreate ,他们都受到这个问题。 想到的一个解决scheme是创build一个能够捕获错误的插件,然后返回find的结果,但是在这里可能会有更好的方法 – 可能是一个我不知道的本地mongoose。

mongoose – 链接承诺

我正在寻找关于如何链接使用MongoDB / mongoose的“查找或创build”function的承诺的build议。 我目前尝试过: userSchema.statics.findByFacebookIdOrCreate = function (facebookId, name, email) { var self = this; return this.findOne({facebookId: facebookId }).exec() .then(function (user) { if (!user) { return self.model.create({ facebookId: facebookId, name: name, email: email }).exec().then(function (user) { return user; }); } return user; }); }; 我从我的(node / express)API端点调用它: User.model.findByFacebookIdOrCreate(fbRes.id, fbRes.name, fbRes.email).then(function (user) { return res.sendStatus(200).send(createTokenForUser(user)); }, […]

链承诺在JavaScript

我创build了许多这样的承诺,以便在我的数据库中创build对象。 var createUserPromise = new Promise( function(resolve, reject) { User.create({ email: 'toto@toto.com' }, function() { console.log("User populated"); // callback called when user is created resolve(); }); } ); 最后,我想按我想要的顺序打电话给我所有的承诺。 (因为有些对象依赖于其他对象,所以我需要保持这个顺序) createUserPromise .then(createCommentPromise .then(createGamePromise .then(createRoomPromise))); 所以我期望看到: User populated Comment populated Game populated Room populated 不幸的是,这个消息被打乱,我不明白是什么。 谢谢

Node.js检测两个mongoose发现完成时

我正尝试用这个库自动完成初始化两个input。 当我加载我的页面时,我将触发一个Ajax来初始化两个input文本。 但是我不知道当我所有的mongoose发现什么时候完成的时候我怎么能发现。 这是我的服务器端代码: app.post('/init/autocomplete', function(req, res){ var autocomplete = { companies: [], offices: [] }; // Find all companies Company.find({}, function(err, companies) { if (err) throw err; companies.forEach(function(company) { autocomplete.companies.push({value: company.name}) }); console.log('One'); }); // Find all offices Office.find({}, function(err, offices) { if (err) throw err; offices.forEach(function(office) { autocomplete.offices.push({value: office.name}) }); console.log('Two'); }); console.log('Three'); […]

在MEAN堆栈中不推荐使用Mongoose的默认承诺库

我试图启动一个MEAN堆栈服务器,但是我得到这个错误信息: Mongoose:mpromise(mongoose的默认承诺库)已被弃用,插入自己的承诺库,而不是: http ://mongoosejs.com/docs/promises.html 我试图在这里search一些答案,但是我发现的答案对我来说还不够清楚: (node:3341)DeprecationWarning:Mongoose:mpromise 我发现文件调用mongoose.connect,但在这个问题上的代码不适合我,任何人都可以解释它是如何工作的?

为什么在调用mongoose Model.create节点时不能链接.catch?

我有一个mongoose模式,并调用Model.create()。 当我在'then'之后链接'catch'时,我得到的未定义不是一个函数,如果我只是将错误函数作为第二个参数调用到'then',那么我不会。 但是当我调用Model.find之类的方法时,我可以使用'catch'。 为什么在调用Model.create时不能链接“catch”? var mySchema = Mongoose.Schema({ name: String, }); 作品: KarmaModel.create({ "name": "ss, }) .then(function() { //do somthing },function()=>{ //do somthing }); 不起作用: KarmaModel.create({ "name": "ss, }) .then(function() { //do somthing }).catch(function()=>{ //do somthing });

如何使用承诺,以避免callback地狱?

所以我有一个Posts集合 { id: String, comments: [String], # id of Comments links: [String], #id of Links } 评论:{id:String,comment:String,} 链接:{id:string,链接:string,} find一个post的评论和链接属于它的ID: Posts.findOne({id: id}, function(post) { Comments.find({id: post.id}, function(comments) { Links.find({id: post.id}, function(links) { res.json({post: post, comments: comment, links: links}) }) }) }) 如何使用Promise( http://mongoosejs.com/docs/promises.html )避免callback地狱? var query = Posts.findOne({id: id}); var promise = query.exec(); promise.then(function (post) { […]