首页 > 解决方案 > .save 不是函数,并且 .findOneAndUpdate() 不会更新我的数据库

问题描述

我正在设置一个后端服务器,我想在路由器中做一个 put 方法。我在后端使用猫鼬快递。

当我尝试使用 .save() 更新数据库中的某些数据时,出现错误:

events.js:174 抛出错误;// 未处理的“错误”事件 ^

TypeError:PC.save 不是函数

我正在尝试使用 .findOneAndUpdate() 进行另一种解决方案,它是成功的,但它不会更新我的数据库。

const express = require('express')
const routes = express()

const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/myapp',  { useNewUrlParser: true });
const db = mongoose.connection;
db.on("error", console.error.bind(console, "connection error"));
db.once("open", function(callback){
  console.log("Connection Succeeded");
});

var PC = require("../models/PC");

//...here is my get delete etc..

这是我使用 findOneAndUpdate 的第一个解决方案

routes.put('/:id', (req, res) => {
 mongoose.set('useFindAndModify', false);
  PC.findOneAndUpdate(
    { 'title': req.body.title }, 
    { '$set': {'description': req.body.description} },
    {'new': true },
    function(err, PC) {
        if (err) {
          console.log('ERROR WHILE PUT PC');
            throw (err);
        } else {
            console.log('Succes set');
            res.status(200).send(PC)
        }
    }
  );
})

这是我的第二个解决方案

routes.put('/:id', (req, res) => {
  PC.findById(req.params.id, 'title description', function (error, pc) {
    if (error) { console.error(error); }

    PC.title = req.body.title
    PC.description = req.body.description
    console.log(PC);
    PC.save(function (error) {
      if (error) {
        console.log(error)
      }
      res.send({
        success: true,
        message: 'PC saved successfully!',
        PC: req.body
      })
    })
  })
})

module.exports = routes;

我的模型:

var mongoose = require("mongoose");
var Schema = mongoose.Schema;

var PCSchema = new Schema({
  id: Number,
  brand: String,
  cpu: String,
  memory: Number,
  type: String,
  vga: String,
  score: Number,
  title: String,
  description: String
});

var PC = mongoose.model("PC", PCSchema);
module.exports = PC;

标签: javascriptnode.jsexpressvue.jsmongoose

解决方案


在您的第一个示例中,您似乎发现了错误的参数,您应该使用id.

尝试findByIdAndUpdate改用:

routes.put('/:id', (req, res) => {
 mongoose.set('useFindAndModify', false);
  PC.findByIdAndUpdate( 
    req.params.id,
    { '$set': {'description': req.body.description, 'title': req.body.title} },
    {'new': true },
    function(err, pc) {
        if (err) {
          console.log('ERROR WHILE PUT PC');
            throw (err);
        } else {
            console.log('Succes set');
            res.status(200).send(pc)
        }
    }
  );
})

在您的第二个示例中,您应该调用.save结果,而不是原始PC Model. 您可以将其更改为:

routes.put('/:id', (req, res) => {
  PC.findById(req.params.id, 'title description', function (error, pc) {
    if (error) { console.error(error); }

    // Use lowercase `pc` on all these lines
    pc.title = req.body.title
    pc.description = req.body.description
    console.log(pc);
    pc.save(function (error) {
      if (error) {
        console.log(error)
      }
      res.send({
        success: true,
        message: 'PC saved successfully!',
        PC: req.body
      })
    })
  })
})

推荐阅读