Mongodb / Node.js findByIdAndUpdate方法未更新

时间:2018-04-08 21:47:01

标签: node.js mongodb

我正在尝试使用findByIdAndUpdate方法更新mongo数据库中的集合。我点击了端点并且发送了更新信息的请求,响应始终是更新前的信息,更新永远不会出现在我的数据库中。

我做错了什么?

以下是我的edit.js文件:

const express = require('express');
const bodyParser = require('body-parser');
const _ = require('lodash');
const dbConfig = require('../mongo/dbConfig');
const Band = require('../mongo/models/band');
const mongoose = require('mongoose');


// Initialize the router
const router = express.Router();
mongoose.connect(dbConfig.devUrl);

//init body parser
router.use(
  bodyParser.urlencoded({
    extended: true
  })
);

router.use(bodyParser.json());

router.post('/band/editProfile', function (req, res) {
  console.log(req.body);
  console.log("it hit !!!!!!!!!!!!!!!!!!");
  Band.findByIdAndUpdate(
    // the id of the item to find

   req.body.id,
    {
      bands: {
        bandName: req.body.bandName,
        genre: req.body.genre,
        numberOfMembers: req.body.numberOfMembers,
        city: req.body.city,
        state: req.body.state,
        zip: req.body.zip,
        travel: req.body.travel,
        travelDistance: req.body.travelDistance,
        spotify: req.body.spotify,
        youtube: req.body.youtube,
        soundcloud: req.body.soundcloud,
        twitter: req.body.twitter,
        facebook: req.body.facebook
      }
    },
    function (err, response) {
      // Handle any possible database errors
      if (err) {
        console.log("we hit an error" + err);
        res.json({
          message: 'Database Update Failure'
        });
      }
      console.log("This is the Response: " + response);
    }
  );
});

这是我的乐队模型:

//Module Imports
const mongoose = require('mongoose');

//Initializes a mongoose schema to be used with the document
const bandSchema = new mongoose.Schema({
  bandName: String,
  genre: String,
  numberOfMembers: Number,
  city: String,
  state: String,
  zip: String,
  travel: Boolean,
  travelDistance: Number,
  spotify: String,
  youtube: String,
  soundcloud: String,
  twitter: String,
  facebook: String
});

//Creates the Schema to be used with the export
let Band = mongoose.model('band', bandSchema);

//Exports the Schema to the API
module.exports = Band;

我已经对此进行了彻底的研究,并且因为任何帮助而感到困惑!

1 个答案:

答案 0 :(得分:0)

bands位于更新对象中,但不在您的架构中。只需删除更新对象的额外图层,即可进行更新。但您还需要将{new: true}选项作为单独的参数包含在内,以便返回更新的文档而不是原始文档(请参阅docs)。

Band.findByIdAndUpdate(
  req.body.id,
  {
    bandName: req.body.bandName,
    genre: req.body.genre,
    numberOfMembers: req.body.numberOfMembers,
    city: req.body.city,
    state: req.body.state,
    zip: req.body.zip,
    travel: req.body.travel,
    travelDistance: req.body.travelDistance,
    spotify: req.body.spotify,
    youtube: req.body.youtube,
    soundcloud: req.body.soundcloud,
    twitter: req.body.twitter,
    facebook: req.body.facebook
  },
  {new: true},
  function (err, response) {
    // Handle any possible database errors
    if (err) {
      console.log("we hit an error" + err);
      res.json({
        message: 'Database Update Failure'
      });
    }
    console.log("This is the Response: " + response);
  }
);