最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

保存不保存任何数据

运维笔记admin19浏览0评论

保存不保存任何数据

保存不保存任何数据

我正在尝试将一些数据保存到本地mongodb数据库中。我的架构如下所示:

const mongoose = require('mongoose');

const userSchema = mongoose.Schema({
    _id: mongoose.Schema.Types.ObjectId,
    name: String,
    email: String,
    passwordHash: String,
    registerTimeStamp: { type: Number, default: Date.now() },
    usersFollowing: [],
    accountStatus: {
        isBanned: { type: Boolean, default: false },
        reason: { type: String, default: '' }
    }
});

module.exports = mongoose.model('User', userSchema);
回答如下:

首先,当你打电话:

user.save(console.log("saved"));

如果您收到错误,控制台将打印“已保存”。所以,如果没有合适的处理程序,你可能会遇到错误。如果您真的想知道您的用户实例发生了什么:

user.save()
.then(() => console.log("saved"))
.catch((error) => console.log(error));

如果你想使用回调而不是promises:

user.save(function(error) {
  if(error) throw error;
  console.log("saved");
});

现在,您的插入方法中存在错误。

更改:

_id: new mongoose.Types.ObjectId(),

附:

_id: new mongoose.Types.ObjectId,

括号是问题。

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论