简体   繁体   English

.save不是函数,而.findOneAndUpdate()不会更新我的数据库

[英].save is not a function, and .findOneAndUpdate() doesn't update my db

I'm setting up a backend server, and i want to do a put method in router. 我正在设置一个后端服务器,我想在路由器中执行put方法。 I'm using mongoose express in backend. 我在后端使用mongoose express。

When i'm trying update some data in my db with .save() , i get error: 当我尝试用.save()更新我的数据库中的一些数据时,我收到错误:

events.js:174 throw er; events.js:174 throw er; // Unhandled 'error' event ^ //未处理的'错误'事件^

TypeError: PC.save is not a function TypeError:PC.save不是函数

I'm trying another soulution with .findOneAndUpdate(), it is success but it doesn't update my database. 我正在尝试使用.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..

This is my first solution with findOneAndUpdate 这是我使用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)
        }
    }
  );
})

And this is my second solution 这是我的第二个解决方案

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;

my model: 我的模特:

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;

In your first example it looks like you are finding with the wrong param, and you should be using id . 在你的第一个例子中,看起来你发现了错误的参数,你应该使用id

Try using findByIdAndUpdate instead: 请尝试使用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)
        }
    }
  );
})

In you second example, you should be calling .save on the result, not the original PC Model . 在第二个示例中,您应该在结果上调用.save ,而不是原始PC Model You could change that to: 您可以将其更改为:

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
      })
    })
  })
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM