摘要:const template = require('art-template'); const path = require('path'); // 时间 const sd = require('silly-datetime'); //设置模板根目录 template.defaults.root =
阅读全文
摘要:一.升级node版本,修改引入方式 //升级到18.x版本后修改引入方式 const querystring= require('node:querystring') 二.官方推荐URLSearchParams替代 因为不想升级就按照文档先用了URLSearchParams const url =
阅读全文
摘要:多集合联合查询(集合关联) 通常不同集合的数据之间是有关系的,例如文章信息和用户信息存储在不同集合中,但文章是某个用户发表的,要查询文章的所有信息包括发表用户,就需要用到集合关联。 const mongoose = require('mongoose'); mongoose.connect('mon
阅读全文
摘要:Mongoose验证 在创建集合规则时,可以设置当前字段的验证规则,验证失败就则输入插入失败。 常见的验证规则: - required: true 必传字段 - minlength:3 字符串最小长度 - maxlength: 20 字符串最大长度 - min: 2 数值最小为2 - max: 10
阅读全文
摘要:向数据库中导入数据 mongoimport -d 数据库名称 -c 集合名称 --file 要导入的文件 mongoimport -d playground -c users --file ./user.json 查询 // 查询用户集合中的所有文档 // User.find().then(resu
阅读全文
摘要:方法1 // 引入mongoose第三方模块 用来操作数据库 const mongoose = require('mongoose'); // 数据库连接 mongoose.connect('mongodb://localhost/playground', { useNewUrlParser: tr
阅读全文
摘要:(node:2072) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client at new NodeError (in
阅读全文