无法使用mongoose将文档保存到mongodb

时间:2019-04-18 15:37:26

标签: node.js express mongoose

我有这样的模式

var Schema = mongoose.Schema;

var schema = new Schema({
    uid : {type: String, required: true},
    title: {type: String, required: true},
    description: {type: String, required: true}
})

module.exports = mongoose.model('Task', schema)

在另一个文件中,我包括了该文件,并尝试为数据库设置种子:

var Task = require('../models/task');
var uuid4 = require('uuid4')
var mongoose = require('mongoose')
mongoose.Promise = global.Promise;

mongoose.connect('mongodb://localhost:27017/app', {useNewUrlParser: true});
var task = new Task({
    uid: uuid4(),
    title: 'task3 name',
    description: 'task3 desc'
})
task.save(function (err) {
     if (err) console.log(err)
  })
}

无论我做什么,都不会保存我想要的数据。 现在,当我进入数据库,应用程序数据库,任务集合时,那边没有插入

版本:“猫鼬”:“ ^ 5.5.2”,

1 个答案:

答案 0 :(得分:0)

根据this documentation

  

如果创建自定义连接,请改用该连接的model()函数。

<link rel="stylesheet" type="text/css" href="style.css">

<div class="flexcontainer">
    <h3 class="heading">Six Types of Concentration</h3>
    <div class="gallery">

        <!-- First Block -->
        <div class="gallery-item">
            <p class="centre-text"><a class="concentration-links" href="https://www.brainzyme.com/products/brainzyme-professional"> Concentration & Focus</a></p>
            <img class="gallery-image" src="gridimages/image1.png" alt="0">
        </div>

        <!-- Second Block -->
        <div class="gallery-item">
            <p class="centre-text"><a class="concentration-links" href="https://www.brainzyme.com/products/brainzyme-cognitive-enhancer-elite">Memory </a></p>
            <img class="gallery-image" src="gridimages/image2.png" alt="0">
        </div>

        <!-- Third Block -->
        <div class="gallery-item">
            <p class="centre-text"><a class="concentration-links" href="https://www.brainzyme.com/products/brainzyme-cognitive-enhancer-elite">Brain Health</a></p>
            <img class="gallery-image" src="gridimages/image3.png" alt="0">
        </div>

        <!-- Fourth Block -->
        <div class="gallery-item">
            <p class="centre-text"><a class="concentration-links" href="https://www.brainzyme.com/products/brainzyme-professional">Nootropics</a></p>
            <img class="gallery-image" src="gridimages/image3.png" alt="0">
        </div>

        <!-- Fifth Block -->
        <div class="gallery-item">
            <p class="centre-text"><a class="concentration-links" href="https://www.brainzyme.com/products/brainzyme-cognitive-enhancer-elite">Mood</a></p>
            <img class="gallery-image" src="gridimages/image1.png" alt="0">
        </div>

        <!-- Sixth Block -->
        <div class="gallery-item">
            <p class="centre-text"><a class="concentration-links" href="https://www.brainzyme.com/products/brainzyme">Energy & Motivation</a></p>
            <img class="gallery-image" src="gridimages/image3.png" alt="0">
        </div>
    </div>
</div>

因此,您需要从连接中声明模型,以便它知道与var connection = mongoose.createConnection('mongodb://localhost:27017/test'); var Tank = connection.model('Tank', yourSchema); 动作相关联的连接。

我建议创建一个专用的.save()文件来处理创建连接的过程,然后可以将其导入到每个模式文件中,以便从提供的连接中创建模型。