大佬教程收集整理的这篇文章主要介绍了创建的模型未显示在 mongoDB 中,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个这样的帐户模型,它运行良好:
const mongoose = require('mongoose');
const scheR_643_11845@a = mongoose.scheR_643_11845@a;
const card = new scheR_643_11845@a(
{
ID: String,perfoRMANce: [number],type: String,reference: [String],hardPronounces: [String],words: [String],translate: String,colorize: [String],image: String,due: number,interact: {
recordTimes: [{ start: number,end: number }],recordingStorage: { x: [number],y: [number] },initialpositions: { left: String,top: String }
}
}
);
const scheR_643_11845@a = new scheR_643_11845@a({
email: { type: String,unique: true,required: true },passwordHash: { type: String,title: { type: String,firstname: { type: String,lastname: { type: String,acceptTerms: Boolean,role: { type: String,verificationToken: String,verifIEd: Date,resetToken: {
token: String,expires: Date
},passwordreset: Date,created: { type: Date,default: Date.Now },updated: Date,userLevel: { type: String,default: 'starter' },cards: {
starter : [card],intermediate : [card],advanced : [card],}
});
scheR_643_11845@a.virtual('isverifIEd').get(function () {
return !!(this.verifIEd || this.passwordreset);
});
scheR_643_11845@a.set('toJsON',{
virtuals: true,versionKey: false,transform: function (doc,ret) {
// remove @R_772_8270@ props when object is serialized
delete ret._ID;
delete ret.passwordHash;
}
});
module.exports = mongoose.model('Account',scheR_643_11845@a);
@H_301_4@
然后我有帮助函数,它允许我使用上面的模型:
const account = await db.Account.findOne({ email });
@H_301_4@
这是辅助函数:
const config = require('config.Json');
const mongoose = require('mongoose');
const connectionoptions = { useCreateIndex: true,useNewUrlParser: true,useUnifIEdtopology: true,useFindAndModify: false };
mongoose.connect(process.env.MONGODB_URI || config.connectionString,connectionoptions);
mongoose.Promise = global.Promise;
module.exports = {
Mother: require('accounts/mother.model'),Account: require('accounts/account.model'),refreshToken: require('accounts/refresh-token.model'),isValIDID
};
function isValIDID(ID) {
return mongoose.Types.ObjectID.isValID(ID);
}
@H_301_4@
问题是如果我创建另一个名为 @H_427_5@mother@H_301_4@ 的模型,我在数据库中看不到新模型:
这是母亲模型:
const mongoose = require('mongoose');
const scheR_643_11845@a = mongoose.scheR_643_11845@a;
const card = new scheR_643_11845@a(
{
ID: String,top: String }
}
}
);
const mother = new scheR_643_11845@a({
cards: {
starter : [card],advanced : [card]
}
});
module.exports = mongoose.model('Mother',mother);
@H_301_4@
这是我在 mangoDB 中看到的:
我不知道如何调试这个请帮忙..
编辑:这是我所拥有的概述:
EDIT2:这是保存功能:
异步函数 saveMotherCard(card,level) {
// card is based on the scheR_643_11845@a
// level is 'starter'
const db = new db.Mother(card[level]);
db.cards[level].push(card);
const a = await db.save();
console.log(a);
@H_301_4@
}
编辑 3:
我使用日志向您展示您的代码不会被删除,因此使用此代码我只能看到第一个控制台日志 1:
console.log('1')
const db = new db.Mother(); // No need to specify the card here when pushing it
console.log('2')
db.cards[level].push(card);
console.log('3')
const a = await db.save();
console.log(a);
@H_301_4@
编辑 4:我的注册功能工作正常,我想这可能会有所帮助:
async function register(params,origin) {
// valIDate
if (await db.Account.findOne({ email: params.email })) {
// send already registered error in email to prevent account enumeration
return await sendAlreadyRegisteredEmail(params.email,origin);
}
// create account object
const account = new db.Account(params);
// first registered account is an admin
const isFirstAccount = (await db.Account.countdocuments({})) === 0;
account.role = isFirstAccount ? Role.admin : Role.User;
account.verificationToken = randomTokenString();
// hash password
account.passwordHash = hash(params.password);
// save account
await account.save();
// send email
await sendVerificationEmail(account,origin);
}
@H_301_4@
由于 const db = await db.Mother.find();
导致一个空数组,您必须为 Mother Model 输入一个条目,以便让 Mother 集合出现在数据库中。
另外为了让您的代码更简洁,因为您在 Account & Mother 中使用了相同的卡片架构,如果您将其放在一个单独的文件中会很棒。
编辑 1:
const db = new db.Mother(); // No need to specify the card here when pushing it
db.cards[level].push(card);
const a = await db.save();
console.log(a);
编辑 2 和 3:
console.log('1')
const newMother = new db.Mother({}); // const name already assigned
console.log('2',newMother)
newMother.cards[level].push(card);
console.log('3',newMother)
const a = await newMother.save();
console.log(a);
以上是大佬教程为你收集整理的创建的模型未显示在 mongoDB 中全部内容,希望文章能够帮你解决创建的模型未显示在 mongoDB 中所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。