【问题标题】:Push data to MongoDB with Mongoose使用 Mongoose 将数据推送到 MongoDB
【发布时间】:2020-11-16 17:31:54
【问题描述】:

我尝试在我的网站上构建一个最喜欢的系统。当用户单击按钮时,它会选择数据并将其推送到后端。这部分有效:

const dataPush = {
                            idSave: idSaveAttr,
                            urlSave: urlSaveAttr,
                            imgSave: imgSave,
                            marqueSave: marqueSave,
                            smSave: smSave,
                            typeSave: typeSave,
                            precisionSave: precisionSave,
                            yearsSave: yearsSave,
                            coteEuSave: coteEuSave,
                            coteUsdSave: coteUsdSave,
                            coteGbSave: coteGbSave,
                            coteChfSave: coteChfSave
                        }
                        console.log(dataPush)
                        //push to backend
                        $.post('/fr/save', dataPush);

在后端,我有一个用户模型:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const passportLocalMongoose = require('passport-local-mongoose');

//Create Schema
const User = new Schema({
    firstName: {
        type: String,
        required: true
    },
    lastName: {
        type: String,
        required: true
    },
    email: {
        type: String,
        required: true,
        unique: true,
        required: true
    }
    favorites: {
        type: [{
            idSave: {
                type: String
            },
            urlSave: {
                type: String
            },
            imgSave: {
                type: String
            },
            marqueSave: {
                type: String
            },
            smSave: {
                type: String
            },
            typeSave: {
                type: String
            },
            precisionSave: {
                type: String
            },
            yearsSave: {
                trype: String
            },
            coteEuSave: {
                type: String,
            },
            coteUsdSave: {
                type: String,
            },
            coteGbSave: {
                type: String,
            },
            coteChfSave: {
                type: String,
            }
        }]
    }
});

User.plugin(passportLocalMongoose);

mongoose.model('users', User);

但是如何将所有这些元素推送到我的 Mongoose DB 关于我的用户模型? 我最喜欢的后端:

//Load User Model
require('../../models/User');
const User = mongoose.model('users');

    router.post('/save', ensureAuthenticated, (req, res) => {
    const dataPush = req.body
    console.log(dataPush)
})
router.get('/save', csrfProtection, (req, res) => {
    res.send('yep')
})

还有console.log的输出:

[Object: null prototype] {
  idSave: '10000',
  urlSave: 'exmplae-of-so.com',
  imgSave: 'https://example.com/models.jpg',
  marqueSave: 'AC',
  smSave: '10 HP',
  typeSave: 'Cabriolet',
  precisionSave: '',
  yearsSave: '1913-1916',
  coteEuSave: '28 035 €',
  coteUsdSave: '$30,590',
  coteGbSave: '£24,911',
  coteChfSave: 'CHF 30’632'
}

【问题讨论】:

  • 看不懂User Object ID
  • 是的,但是我怎样才能找回它(如果我有多个用户)?
  • 我确保我的用户已通过身份验证,因此我不确定是否需要检索它。但是当我只使用 User.update({$push: {"favorites.type": req.body}}) 时,我的 MongoDB 数据库中没有我最喜欢的
  • 也许我的错误是我的 MongoDB 数据库中还没有 idSave 如果这是第一个要添加的最爱?
  • 请解释一下这个问题..

标签: javascript mongodb mongoose


【解决方案1】:

我不确定您的问题,但根据您的评论,我建议,如果最喜欢的是新的,则添加,如果已经可用,则更新。

  • 假设这是您的初始变量,
let userId = mongoose.Types.ObjectId("123");
let data = req.body;
  • 如果用户正在添加新的收藏夹:
if (!data.idSave) {
    User.update(
      { _id: userId }, 
      {
        $push: {
          "favorites.type": data
        }
      }
    )
}
  • 如果用户正在更新现有收藏夹
else {
    User.update(
      {
        _id: userId, // optional 
        "favorites.type.idSave": data.idSave
      }, 
      {
        $set: {
          "favorites.type.$": data
        }
      }
    )
}

【讨论】:

  • 我了解您的代码,但是当我使用它时没有任何反应。我不知道为什么。
猜你喜欢
  • 2021-02-17
  • 1970-01-01
  • 2020-06-04
  • 2019-08-23
  • 2019-03-28
  • 1970-01-01
  • 2019-04-16
  • 1970-01-01
  • 2021-10-17
相关资源
最近更新 更多