Skip to content

Instantly share code, notes, and snippets.

@crguezl
Created April 14, 2016 18:50
Show Gist options
  • Save crguezl/39f8d635917f7a3b5164a817ef65095b to your computer and use it in GitHub Desktop.
Save crguezl/39f8d635917f7a3b5164a817ef65095b to your computer and use it in GitHub Desktop.
Ejemplo del uso de Promesas en Mongoose
(function() {
"use strict";
const util = require('util');
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/chuchu');
const CardSchema = mongoose.Schema({
"rank" : String,
"suit" : String,
"chuchu": [ {a: String, b: String}]
});
const Card = mongoose.model("Card", CardSchema);
let c1 = new Card({"rank":"ace", "suit":"spades ♠", "chuchu": [ {a: "hello", b: "world!"}]});
let c2 = new Card({"rank":"2", "suit":"hearts ♥", "chuchu": [{a: "hola", b: "mundo"}]});
let c3 = new Card({"rank":"3", "suit":"clubs ♣", "chuchu": [{a: "hola", b: "mundo"}]});
let c4 = new Card({"rank":"4", "suit":"diamonds ♦", "chuchu": [{a: "hola", b: "mundo"}]});
let p1 = c1.save(function (err) {
if (err) { console.log(`Hubieron errores:\n${err}`); return err; }
console.log(`Saved: ${c1}`);
});
let p2 = c2.save(function (err) {
if (err) { console.log(`Hubieron errores:\n${err}`); return err; }
console.log(`Saved: ${c2}`);
});
let p3 = Card.create(c3, function (err, x) {
if (err) { console.log(`Hubieron errores:\n${err}`); return err; }
console.log(`Saved p3: ${x}`);
});
Promise.all([p1, p2, p3]).then( (value) => {
console.log(util.inspect(value, {depth: null}));
mongoose.connection.close();
});
})();
@crguezl
Copy link
Author

crguezl commented Apr 14, 2016

Se ilustran:

  • Las funciones savey create
  • Las promesas
  • Como abrir una conexión con la base de datos y como cerrarla

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment