猫鼬为一个请求保存两次数据

时间:2018-11-24 01:44:19

标签: javascript node.js express mongoose

我对mongodb和express比较陌生,我希望将通过api调用检索到的数据保存到我的数据库中。由于某种原因,我的服务器为一个get请求两次保存了两次数据(创建两个具有相同详细信息但ID不同的文档),但我不知道为什么

const log = console.log;

const express = require('express')
const port = process.env.PORT || 8000
const movieServer = require('./movie-getter')
const { Movie } = require('./model/Movie')
const mongoose = require('mongoose')
mongoose.connect('mongodb://localhost:27017/ConspireView', { useNewUrlParser: true});


const app = express()

app.get('/movie/:name/:year', (req, res) => {
    const name = req.params.name
    const year = req.params.year
    // let movieObject
    movieServer.getMovie(name, year).then((result) => {

        new Movie({
            name: result.title,
            year: result.release_date,
            poster: result.poster_path,
            banner: result.backdrop_path,
            numOfDiscussions: 0,
            numOfComments: 0,
            vote_average: 0
            // discussions: null
        }).save().then(result => {
            res.send(result)
        })        
    }).catch((error) => {
        log(error)
    }) 
})

这里有语法错误吗?

0 个答案:

没有答案