英文:
TypeError: seed is not a function
问题
我正在工作一个涉及旅行网站的作业,并创建一个存储代码、名称和长度等信息的数据库。我尝试向数据库中添加数据,但出现了一个错误,错误消息如下:
TypeError: seed is not a function
at main (C:\Users\marce\src\travlr\app_server\models\db.js:30:11)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
C:\Users\marce\src\travlr\node_modules\mongoose\lib\connection.js:972
throw a MongooseError ('Connection.prototype.close() no longer accepts a callback');
我认为我通过终端安装了 seedgoose。问题可能出在哪里?以下是我的代码的一部分:
db.js
const mongoose = require('mongoose');
const host = process.env.DB_HOST || '127.0.0.1';
const conn_uri = mongodb://${host}/travlr
;
const { seed } = require('./seed');
// 注册模型
require('./trips');
mongoose.connection.on('connected', () => console.log('已连接!'));
mongoose.connection.on('error', err => console.log(err));
mongoose.connection.on('disconnected', () => console.log('已断开连接!'));
mongoose.set('strictQuery', false);
// 在退出应用程序之前关闭 MongoDB 连接
const gracefulShutdown = (msg, callback) => {
mongoose.connection.close(() => {
console.log(由于 ${msg} 断开 Mongoose 连接
);
callback();
});
}
process.once('SIGUSR2', () =>
gracefulShutdown('nodemon 重新启动', () => process.kill(process.pid, 'SIGUSR2')));
process.on('SIGINT', () =>
gracefulShutdown('应用程序终止', () => process.exit(0)));
async function main() {
await mongoose.connect(conn_uri);
await seed();
}
main().catch(console.log);
seed.js
const fs = require('fs');
const path = require('path');
const mongoose = require('mongoose');
const seed = async function() {
// 种子 TRIPS
const trips = JSON.parse(fs.readFileSync(path.join(__dirname, '../../data/trips.json'), 'utf8'));
const trip = mongoose.model('trips');
await trip.deleteMany();
await trip.insertMany(trips);
module.exports = { seed };
}
英文:
I am currently working on an assignment that involves a traveler website and creating a database that stores things like code, name, and length. I am trying to seed data into a database, but I am getting an error that says:
TypeError: seed is not a function
at main (C:\Users\marce\src\travlr\app_server\models\db.js:30:11)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
C:\Users\marce\src\travlr\node_modules\mongoose\lib\connection.js:972
throw new MongooseError('Connection.prototype.close() no longer accepts a callback');
I thought I installed seedgoose using my terminal. What could be the problem? Here is part of my code:
db.js
const mongoose = require('mongoose');
const host = process.env.DB_HOST || '127.0.0.1';
const conn_uri = `mongodb://${host}/travlr`;
const {seed} = require('./seed');
// Register models
require('./trips');
mongoose.connection.on('connected', () => console.log('CONNECTED!'));
mongoose.connection.on('error', err => console.log(err));
mongoose.connection.on('disconnected', () => console.log('DISCONNECTED!'));
mongoose.set('strictQuery', false);
// Kill MongoDB connections before exiting app
const gracefulShutdown = (msg, callback) => {
mongoose.connection.close( () => {
console.log(`Mongoose disconnected due to ${msg}`);
callback();
});
}
process.once('SIGUSR2', () =>
gracefulShutdown('nodemon restart', () => process.kill(process.pid, 'SIGUSR2')));
process.on('SIGINT', () =>
gracefulShutdown('app termination', () => process.exit(0)));
async function main() {
await mongoose.connect(conn_uri);
await seed();
}
main().catch(console.log);
seed.js
const fs = require('fs');
const path = require('path');
const mongoose = require('mongoose');
const seed = async function() {
// Seed TRIPS
const trips = JSON.parse(fs.readFileSync(path.join(_dirname, '../../data/trips.json'), 'utf8'));
const trip = mongoose.model('trips');
await trip.deleteMany();
await trip.insertMany(trips);
module.exports = { seed };
}
答案1
得分: -1
module.exports = { seed }; 将其移出函数范围有所帮助,并添加另一个下划线到 _dirname 有所帮助。
const seed = async function() {
// 种子 TRIPS
const trips = JSON.parse(fs.readFileSync(path.join(__dirname, '../../data/trips.json'), 'utf8'));
const trip = mongoose.model('trips');
await trip.deleteMany();
await trip.insertMany(trips);
}
module.exports = { seed };
英文:
Moving the module.exports = { seed }; outside of the function scope helped and adding another underscore to _dirname helped.
const seed = async function() {
// Seed TRIPS
const trips = JSON.parse(fs.readFileSync(path.join(__dirname, '../../data/trips.json'), 'utf8'));
const trip = mongoose.model('trips');
await trip.deleteMany();
await trip.insertMany(trips);
}
module.exports = { seed };
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论