Node.js Mongoose Model.create()未保存文档
所以我正在使用Node、Mongo、Mongoose、commander.js和inquirer.js开发控制台CRUD应用程序 在app.js中,Node.js Mongoose Model.create()未保存文档,node.js,mongoose,Node.js,Mongoose,所以我正在使用Node、Mongo、Mongoose、commander.js和inquirer.js开发控制台CRUD应用程序 在app.js中,addItem()是一个返回Model.create({obj})的函数,根据Mongoose文档,它应该返回一个承诺。但是,当我尝试在下一个then()中记录resp时,我没有得到任何输出。在使用不同的模型之前,它工作得很好,但现在不是这样了。我感觉create()不起作用(我知道它不起作用,因为数据库中没有创建集合),但我甚至不知道如何记录错误。
addItem()
是一个返回Model.create({obj})的函数,根据Mongoose文档,它应该返回一个承诺。但是,当我尝试在下一个then()
中记录resp时,我没有得到任何输出。在使用不同的模型之前,它工作得很好,但现在不是这样了。我感觉create()
不起作用(我知道它不起作用,因为数据库中没有创建集合),但我甚至不知道如何记录错误。我已经在这个问题上纠缠了一两天,似乎没有其他人有过这个问题;或者至少我不知道如何正确地界定问题
app.js
#!/usr/bin/env node
const mongoose = require('mongoose');
mongoose.Promise = Promise;
const program = require('commander');
const { prompt } = require('inquirer');
const {addItem, getItem, removeItem, makeArray} = require('./logic.js');
const {itemTypeQuestion, papyriQuestions, lampQuestions} = require('./questions');
const typeQuestions = (type) => {
if(type === 'papyrus') {
return prompt(papyriQuestions).then(resp => {resp.item_type = type; return resp})
} else if (type === 'lamp') {
return prompt(lampQuestions).then(resp => {resp.item_type = type; return resp})
}
}
program
.command('addItem')
.alias('a')
.description('Add an item')
.action( () => {
prompt(itemTypeQuestion)
.then(resp => typeQuestions(resp.item_type))
.then(answers => makeArray(answers, "more_ids", "other_ids", ", ") )
.then(resp => addItem(resp))
.then(resp => console.info(resp))
.then(mongoose.disconnect())
.catch(e => {
console.error(e);
mongoose.disconnect();
})
} )
const mongoose = require('mongoose');
mongoose.Promise = Promise;
mongoose.connect('mongodb://localhost:27017/museum_catalog', { useNewUrlParser: true });
const {Papyrus, Lamp} = require('./schema.js');
/**
* @function [addItem]
* @returns {Promise} Promise
*/
const addItem = (item) => {
if (item.item_type === 'papyrus') {
return Papyrus.create(item)
} else if (item.item_type === 'lamp') {
return Lamp.create(item)
}
}
const makeArray = (input, field, newKey, separator) => {
input[newKey] = [];
if (input[field] != 0) {
input[field].split(separator).forEach(element => {
input[newKey].push(element)
});
}
return input;
}
module.exports = {addItem, getItem, removeItem, makeArray}
const mongoose = require('mongoose');
// https://www.npmjs.com/package/mongoose-extend-schema
function extendSchema (Schema, definition, options) {
return new mongoose.Schema(
Object.assign({}, Schema.obj, definition),
options
);
}
// Define Item schema
const itemSchema = mongoose.Schema({
primary_id: {type: String, required: true, lowercase: true, unique: true, trim: true, index: true},
other_ids: [],
item_type: {type: String, required: true, lowercase: true, trim: true},
description: {type: String, lowercase: true, trim: true},
date_created: { type: Date, default: Date.now },
hidden: {type: Boolean, default: false},
height: Number,
width: Number
})
// Papyrus schema & model
const papyrusSchema = extendSchema(itemSchema, {
item_type: {type: String, default: 'papyrus', lowercase: true},
language: {type: String, required: true, lowercase: true, trim: true},
});
const Papyrus = mongoose.model('papyrus', papyrusSchema);
// Lamp schema & model
const lampSchema = extendSchema(itemSchema, {
item_type: {type: String, default: 'lamp', lowercase: true},
isInscription: Boolean,
language: {type: String, lowercase: true, trim: true},
})
const Lamp = mongoose.model('lamps', lampSchema);
module.exports = {Papyrus, Lamp};
logic.js
#!/usr/bin/env node
const mongoose = require('mongoose');
mongoose.Promise = Promise;
const program = require('commander');
const { prompt } = require('inquirer');
const {addItem, getItem, removeItem, makeArray} = require('./logic.js');
const {itemTypeQuestion, papyriQuestions, lampQuestions} = require('./questions');
const typeQuestions = (type) => {
if(type === 'papyrus') {
return prompt(papyriQuestions).then(resp => {resp.item_type = type; return resp})
} else if (type === 'lamp') {
return prompt(lampQuestions).then(resp => {resp.item_type = type; return resp})
}
}
program
.command('addItem')
.alias('a')
.description('Add an item')
.action( () => {
prompt(itemTypeQuestion)
.then(resp => typeQuestions(resp.item_type))
.then(answers => makeArray(answers, "more_ids", "other_ids", ", ") )
.then(resp => addItem(resp))
.then(resp => console.info(resp))
.then(mongoose.disconnect())
.catch(e => {
console.error(e);
mongoose.disconnect();
})
} )
const mongoose = require('mongoose');
mongoose.Promise = Promise;
mongoose.connect('mongodb://localhost:27017/museum_catalog', { useNewUrlParser: true });
const {Papyrus, Lamp} = require('./schema.js');
/**
* @function [addItem]
* @returns {Promise} Promise
*/
const addItem = (item) => {
if (item.item_type === 'papyrus') {
return Papyrus.create(item)
} else if (item.item_type === 'lamp') {
return Lamp.create(item)
}
}
const makeArray = (input, field, newKey, separator) => {
input[newKey] = [];
if (input[field] != 0) {
input[field].split(separator).forEach(element => {
input[newKey].push(element)
});
}
return input;
}
module.exports = {addItem, getItem, removeItem, makeArray}
const mongoose = require('mongoose');
// https://www.npmjs.com/package/mongoose-extend-schema
function extendSchema (Schema, definition, options) {
return new mongoose.Schema(
Object.assign({}, Schema.obj, definition),
options
);
}
// Define Item schema
const itemSchema = mongoose.Schema({
primary_id: {type: String, required: true, lowercase: true, unique: true, trim: true, index: true},
other_ids: [],
item_type: {type: String, required: true, lowercase: true, trim: true},
description: {type: String, lowercase: true, trim: true},
date_created: { type: Date, default: Date.now },
hidden: {type: Boolean, default: false},
height: Number,
width: Number
})
// Papyrus schema & model
const papyrusSchema = extendSchema(itemSchema, {
item_type: {type: String, default: 'papyrus', lowercase: true},
language: {type: String, required: true, lowercase: true, trim: true},
});
const Papyrus = mongoose.model('papyrus', papyrusSchema);
// Lamp schema & model
const lampSchema = extendSchema(itemSchema, {
item_type: {type: String, default: 'lamp', lowercase: true},
isInscription: Boolean,
language: {type: String, lowercase: true, trim: true},
})
const Lamp = mongoose.model('lamps', lampSchema);
module.exports = {Papyrus, Lamp};
schema.js
#!/usr/bin/env node
const mongoose = require('mongoose');
mongoose.Promise = Promise;
const program = require('commander');
const { prompt } = require('inquirer');
const {addItem, getItem, removeItem, makeArray} = require('./logic.js');
const {itemTypeQuestion, papyriQuestions, lampQuestions} = require('./questions');
const typeQuestions = (type) => {
if(type === 'papyrus') {
return prompt(papyriQuestions).then(resp => {resp.item_type = type; return resp})
} else if (type === 'lamp') {
return prompt(lampQuestions).then(resp => {resp.item_type = type; return resp})
}
}
program
.command('addItem')
.alias('a')
.description('Add an item')
.action( () => {
prompt(itemTypeQuestion)
.then(resp => typeQuestions(resp.item_type))
.then(answers => makeArray(answers, "more_ids", "other_ids", ", ") )
.then(resp => addItem(resp))
.then(resp => console.info(resp))
.then(mongoose.disconnect())
.catch(e => {
console.error(e);
mongoose.disconnect();
})
} )
const mongoose = require('mongoose');
mongoose.Promise = Promise;
mongoose.connect('mongodb://localhost:27017/museum_catalog', { useNewUrlParser: true });
const {Papyrus, Lamp} = require('./schema.js');
/**
* @function [addItem]
* @returns {Promise} Promise
*/
const addItem = (item) => {
if (item.item_type === 'papyrus') {
return Papyrus.create(item)
} else if (item.item_type === 'lamp') {
return Lamp.create(item)
}
}
const makeArray = (input, field, newKey, separator) => {
input[newKey] = [];
if (input[field] != 0) {
input[field].split(separator).forEach(element => {
input[newKey].push(element)
});
}
return input;
}
module.exports = {addItem, getItem, removeItem, makeArray}
const mongoose = require('mongoose');
// https://www.npmjs.com/package/mongoose-extend-schema
function extendSchema (Schema, definition, options) {
return new mongoose.Schema(
Object.assign({}, Schema.obj, definition),
options
);
}
// Define Item schema
const itemSchema = mongoose.Schema({
primary_id: {type: String, required: true, lowercase: true, unique: true, trim: true, index: true},
other_ids: [],
item_type: {type: String, required: true, lowercase: true, trim: true},
description: {type: String, lowercase: true, trim: true},
date_created: { type: Date, default: Date.now },
hidden: {type: Boolean, default: false},
height: Number,
width: Number
})
// Papyrus schema & model
const papyrusSchema = extendSchema(itemSchema, {
item_type: {type: String, default: 'papyrus', lowercase: true},
language: {type: String, required: true, lowercase: true, trim: true},
});
const Papyrus = mongoose.model('papyrus', papyrusSchema);
// Lamp schema & model
const lampSchema = extendSchema(itemSchema, {
item_type: {type: String, default: 'lamp', lowercase: true},
isInscription: Boolean,
language: {type: String, lowercase: true, trim: true},
})
const Lamp = mongoose.model('lamps', lampSchema);
module.exports = {Papyrus, Lamp};
也许我没有正确理解承诺或者猫鼬是如何工作的。此外,任何关于其他事情的一般性反馈都是受欢迎的
*编辑-固定8/18/18
在schema.js中我添加了
const testItem = { primary_id: 'Je.41',
more_ids: 'C90',
description: 'test',
height: '12',
width: '12',
hidden: false,
language: 'test',
item_type: 'papyrus',
other_ids: [ 'C90' ] }
Papyrus.create(testItem).then(resp => {console.log(resp)}).catch(e => {console.error(e)})
要使其在启动时创建项目并收到此错误:
MongoError: server instance pool was destroyed
显然,应用程序的断开太快了。在app.js中,我改变了。然后(mongoose.disconnect())
。然后(resp=>mongoose.disconnect())
,它成功了
我还是不确定
disconnect()
仍在努力找出这些承诺。您能提供一些控制台输出,让我们更好地帮助您吗?
。然后(resp=>console.info(resp))
不记录任何内容(即使未定义,它也不会运行)。我期望类似于:{primary\u id:'some id',item\u type:'papyrus',…}
您可以通过添加catch
子句来记录错误Papyrus.create(item).catch(e=>{console.log(e);})
和Lamp.create(item).catch(e=>{console.log(e);})
。希望这能有所帮助。从未记录任何错误。