Question: What is a promise?
A promise is an abstraction for asynchronous programming.
It's an object that proxies for the return value thrown by a function that has to do some asynchronous processing.
Question: How does promise work?
The Promise constructor takes a function (an executor) that will be executed immediately and passes in two functions: resolve , which must be called when the Promise is resolved (passing a result), and reject , when it is rejected (passing an error)
Question: Give example of promise work in mongoose?
User.update({id: 100}, {$inc: { views: 1 }}).limit(1).exec();Here execute multiple function in one line is promise.
Question: How to setup mongoose with promise?
var mongoose = require('mongoose'); mongoose.Promise = require('bluebird'); mongoose.connect('mongodb://localhost:27017/dbname'); var db = mongoose.connection; db.on('error', console.error.bind(console, 'connection error:')); db.once('open', function() { console.log("MongoDB connected Successfully.!"); });
Question: How to Add the data in mongoDb with mongoose?
//Ajax chat messages var AjaxChatMessageSchema = mongoose.Schema({ id: Number, userID: Number, userName: String, userRole: String, channel: String, dateTime: String, ip: String, text: String, status: String, vid: Number }, {collection: 'ajax_chat_messages'}); var AjaxChatMessage = mongoose.model('ajax_chat_messages', AjaxChatMessageSchema); var saveData={id:11, userName:'testuser',text:'hello user'} var AjaxChatMessageDtls = new AjaxChatMessage(saveData); AjaxChatMessageDtls.save(function(err) { if (err) { console.log("Error while saving the details"); response.success = 0; res.send(response); } else { response.success = 1; res.send(response); } });
Question: How to do decrement the value by 1?
AjaxChatMessage.update({id: 100}, {$inc: { views: -1 }}).limit(1).exec();
Question: From where we can read more about promise?
http://mongoosejs.com/docs/promises.html