猫鼬的异步/等待功能

时间:2019-06-20 10:36:31

标签: asynchronous async-await

const mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/mongo-games')
    .then(() => console.log('Now connected to MongoDB!'))
    .catch(err => console.error('Something went wrong', err));

const gameSchema = new mongoose.Schema({
    title: String,
    publisher: String,
    tags: [String],
    date: {
        type: Date,
        default: Date.now
    },
    onSale: Boolean,
    price: Number
});

const Game = mongoose.model('Game', gameSchema);

async function saveGame() {
    const game = new Game({
        title: "The Legend of Zelda: Breath of the Wild",
        publisher: "Nintendo",
        tags: ["adventure", "action"],
        onSale: false,
        price: 59.99,
    });

    const result = await game.save();
    console.log(result);
}

saveGame();

我对代码的怀疑是:在此代码中如何使用async / await函数。这有什么用。不使用它们就不能做到这一点。

SOurce:https://vegibit.com/mongoose-crud-tutorial/

0 个答案:

没有答案