gpt4 book ai didi

node.js - 首先加载特定的 Mongoose 模型

转载 作者:太空宇宙 更新时间:2023-11-04 02:13:33 24 4
gpt4 key购买 nike

我正在尝试在我的“喜欢”模型类(下面的代码)中使用模型(“目标”)。然而,每次我启动 Node 实例时,它都会说 Goal.findById 不是一个函数。

在每个模型中运行 console.log(number) 后,我发现它们的加载非常奇怪,并且不符合我想要/需要的顺序。

我想知道如何在其他人之前加载模型或为模型设置特定的加载顺序?

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var User = require('../models/user.js');
var Goal = require('../models/goal.js');

var likeSchema = new Schema({
userPosted: {
type: Number,
ref: 'user',
required: true
},
goal: {
type: Number,
ref: 'goal',
required: true
}},
{
timestamps: true
});

likeSchema.post('save', function (doc, next) {
var goalID = doc.goal;

Goal.findOne({'_id': doc.goal}, function(err, goal) {
goal.likes.push(doc._id);
goal.save();
User.findById(doc.userPosted, function(err, user) {
user.likedPosts.push(goalID);
user.save();
next();
});
});
});

likeSchema.post('remove', function(doc) {
Goal.findById(doc.goal, function(err, goal) {
goal.likes.pull(doc._id);
goal.save();

User.findById(doc.userPosted, function(err, user) {
user.likedPosts.pull(goal._id);
user.save();
});
});
});

console.log("4");

module.exports = mongoose.model('like', likeSchema);

错误:

TypeError: Goal.findOne is not a function at model.<anonymous> (E:\Project\like.js:28:10)

**编辑:** goal.js

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var autoIncrement = require('mongoose-auto-increment');
var User = require('../models/user.js');
var Like = require('../models/like.js');

var goal = new Schema({
user: {
type: Number,
ref: 'user',
required: true
},
user_name: {
type: String,
required: true
},
title: {
type: String,
required: true,
trim: true
},
description: {
type: String,
default: undefined,
trim: true
},
location: {
type: String,
default: undefined
},
likes: [{
type: Schema.ObjectId,
ref: 'like'
}],
comments: [{
type: Schema.ObjectId,
ref: 'comment'
}],
updates: [{
type: Schema.ObjectId,
ref: 'update'
}],
created: {
type: Date,
default: Date.now
},
cover_image: {
type: String,
default: undefined
},
complete_by: {
type: String,
default: "Death"
},
completed: {
type: Boolean,
default: false
},
completedDate: {
type: String,
default: undefined
},
url: {
type: String,
default: undefined
},
sponsor: {
type: String,
default: undefined
},
private: {
type: Boolean,
default: false
}
});

goal.plugin(autoIncrement.plugin, 'goal');

goal.pre('save', function (next) {
this.wasNew = this.isNew;
next();
});

goal.post('save', function (doc) {
if (this.wasNew) {
User.findById(doc.user, function (err, user) {
user.goals.push(doc._id);
user.save();
});
}
});

goal.post('remove', function(doc) {
//TODO: Removes like objects/user profile/anything containing the goal
User.findById(doc.user, function(err, user) {
user.goals.pull(doc._id);
user.save();
});

Like.find({
'goal': doc._id
}).remove(function(err, removed) {
if(err) {
console.log("ERROR?");
return;
}
console.log('removed likes - ' + removed);
});
});

module.exports = mongoose.model('goal', goal);

最佳答案

我认为你的likeschema会是这样的 -

var likeSchema = new Schema({
userPosted: {
type: Number,
ref: 'User',
required: true
},
goal: {
type: Number,
ref: 'Goal',
required: true
}},
{
timestamps: true
});

如果不起作用,您能否显示您的 Goal 模型代码?

关于node.js - 首先加载特定的 Mongoose 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36879983/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com