简体   繁体   English

MongoDB和Cosmos DB密钥错误收集

[英]MongoDB and Cosmos DB key error collection


I'm trying to create my first MongoDB app with Express & Angular & Azure Cosmos DB. 我正在尝试使用Express&Angular和Azure Cosmos DB创建我的第一个MongoDB应用程序。
Here is my js files: 这是我的js文件:

hero.model.js hero.model.js
  const mongoose = require('mongoose'); const Schema = mongoose.Schema; const heroSchema = new Schema({ id: { type: Number, required: true, unique: true }, name: String }, { collection: 'Heroes' }) const Hero = mongoose.model('Hero', heroSchema); module.exports = Hero; 
mongo.js mongo.js
   const express = require('express');
    const bodyParser = require('body-parser');
    const path = require('path');
    const routes = require('./routes');
    const root = './';
    const port = process.env.PORT || '3000';
    const app = express();
    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({ extended: false }));
    app.use(express.static(path.join(root, 'docs')));
    app.use('/api', routes);
    app.get('*', (req, res) => {
    res.sendFile('docs/index.html', {root});
    });
index.js index.js
  const express = require('express'); const bodyParser = require('body-parser'); const path = require('path'); const routes = require('./routes'); const root = './'; const port = process.env.PORT || '3000'; const app = express(); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: false })); app.use(express.static(path.join(root, 'docs'))); app.use('/api', routes); app.get('*', (req, res) => { res.sendFile('docs/index.html', {root}); }); 
route.js route.js
  const express = require('express'); const router = express.Router(); const heroService = require('./hero.service'); router.get('/heroes', (req, res) => { heroService.getHeroes(req, res); }); router.post('/hero', (req, res) => { heroService.postHero(req, res); }); router.put('/hero/:id', (req, res) => { heroService.putHero(req, res); }); router.delete('/hero/:id', (req, res) => { heroService.deleteHero(req, res); }); module.exports = router; app.listen(port, () => console.log(`API running on localhost:${port}`)); 
hero.service.js hero.service.js
  const Hero = require('./hero.model'); require('./mongo').connect(); function getHeroes(req, res) { const docquery = Hero.find({}); docquery .exec() .then(heroes => { res.status(200).json(heroes); }) .catch(error => { res.status(500).send(error); return; }); } function postHero(req, res) { const originalHero = { id: req.body.id, name: req.body.name }; const hero = new Hero(originalHero); hero.save(error => { if (checkServerError(res, error)) return; res.status(201).json(hero); console.log('Hero created successfully!'); }); } function checkServerError(res, error) { if (error) { res.status(500).send(error); return error; } } function putHero(req, res) { const originalHero = { id: parseInt(req.params.id, 10), name: req.body.name }; Hero.findOne({ id: originalHero.id }, (error, hero) => { if (checkServerError(res, error)) return; if (!checkFound(res, hero)) return; hero.name = originalHero.name; hero.save(error => { if (checkServerError(res, error)) return; res.status(200).json(hero); console.log('Hero updated successfully!'); }); }); } function deleteHero(req, res) { const id = parseInt(req.params.id, 10); Hero.findOneAndRemove({ id: id }) .then(hero => { if (!checkFound(res, hero)) return; res.status(200).json(hero); console.log('Hero deleted successfully!'); }) .catch(error => { if (checkServerError(res, error)) return; }); } function checkFound(res, hero) { if (!hero) { res.status(404).send('Hero not found.'); return; } return hero; } module.exports = { getHeroes, postHero, putHero, deleteHero }; 

It only works when I POST a new hero for the first time and a second time gives me an error: E11000 duplicate key error collection: admin.Heroes Failed _id or unique key constraint. 仅当我第一次发布新英雄,第二次给我一个错误时,它才起作用:E11000重复键错误集合:admin.Heroes失败的_id或唯一键约束。

Please help!! 请帮忙!!

Thanks. 谢谢。

I know it's been a while but I've encountered the same problem. 我知道已经有一段时间了,但是我遇到了同样的问题。 The solution is avoid using the field name id, rename it to UID or something. 解决方案是避免使用字段名称id,将其重命名为UID或其他名称。 Once you done that you have to remove the entire collection and restart. 完成后,您必须删除整个集合并重新启动。

If you are following the Microsoft CosmosDB Angular tutorial, you can clone the repo below and test it on your local. 如果您遵循Microsoft CosmosDB Angular教程,则可以在下面克隆存储库并在本地进行测试。

https://github.com/Azure-Samples/angular-cosmosdb https://github.com/Azure-Samples/angular-cosmosdb

If you are using a newer version of mongoose you have to upgrade the connection string below. 如果您使用的是更新版本的猫鼬,则必须升级以下连接字符串。

         //useMongoClient: true
         useNewUrlParser: true 

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

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