Created
April 14, 2016 18:50
-
-
Save crguezl/39f8d635917f7a3b5164a817ef65095b to your computer and use it in GitHub Desktop.
Ejemplo del uso de Promesas en Mongoose
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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(); | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Se ilustran:
save
ycreate