使用文档但完全混淆......猫鼬模型#save
Using the docs but completely confused... mongoose Model#save
我正在尝试 return 特定的状态代码,例如 409 冲突。我用过 Model#save docs
编辑:我不是要解决错误,这是故意的。
根据文档,回调中有三个参数:err、product 和 numAffected。
编辑:我写错了这段代码并进行了编辑。不管怎样,我从 Ryan 那里得到了很好的回答。
app.post('/skill', (req, res) => {
const skill = new Skill({some_duplicate_object});
skill.save((err, product, numAffected) => {
console.log("Error: ", err);
});
不是我的 console.log,在 Mocha 测试 cli 中,我得到一个错误:
(node:19760) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 5): ValidationError: Path `name` is required.
通过玩耍和运气,我做到了:这不在 mongoose 文档中,这是我写这篇文章的主要原因 post.
app.post('/skill', (req, res) => {
const skill = new Skill({});
skill.save()
.then((err, product, numAffected) => {
console.log("Nothing displayed here");
}, (err) => {
console.log(err.errors);
});
虽然这不在文档中,但它显示了我想要的错误。作为一个真正尝试更多使用官方文档的人,我发现很难理解到底发生了什么。为什么这行得通,如果它在文档中,该信息会在哪里?
{ name:
{ MongooseError: Path `name` is required.
at ValidatorError (/home/codeamend/Coding/projects/portfolio/work/CodeAmend.Com/backend/node_modules/mongoose/lib/error/validator.js:24:11)
at validate (/home/codeamend/Coding/projects/portfolio/work/CodeAmend.Com/backend/node_modules/mongoose/lib/schematype.js:706:13)
at /home/codeamend/Coding/projects/portfolio/work/CodeAmend.Com/backend/node_modules/mongoose/lib/schematype.js:752:11
at Array.forEach (native)
at SchemaString.SchemaType.doValidate (/home/codeamend/Coding/projects/portfolio/work/CodeAmend.Com/backend/node_modules/mongoose/lib/schematype.js:712:19)
at /home/codeamend/Coding/projects/portfolio/work/CodeAmend.Com/backend/node_modules/mongoose/lib/document.js:1408:9
at _combinedTickCallback (internal/process/next_tick.js:73:7)
at process._tickCallback (internal/process/next_tick.js:104:9)
message: 'Path `name` is required.',
name: 'ValidatorError',
properties:
{ type: 'required',
message: 'Path `{PATH}` is required.',
validator: [Function],
path: 'name',
value: undefined },
kind: 'required',
path: 'name',
value: undefined,
reason: undefined } }
额外信息:
"devDependencies": {
"chai": "^3.5.0",
"mocha": "^2.4.5",
"request": "^2.81.0",
"supertest": "^3.0.0"
},
"dependencies": {
"body-parser": "^1.17.1",
"express": "^4.15.2",
"mongoose": "^4.9.2"
}
您没有处理承诺拒绝。
改变这个:
.then((err, product, numAffected) => {
console.log("Error: ", err);
});
对此:
.then((result) => {
console.log('Result:', result);
}).catch((error) => {
console.log('Error:', error);
当然可以根据需要更改回调中发生的内容。
有关未处理拒绝的更多一般信息,请参阅此答案:
你的问题是双重的,你得到的两个错误都确切地告诉你哪里出了问题。你麻烦的根源是缺乏理解(不是试图挑剔你)。这有点像你是电子学初学者,我告诉你电子管偏置不当,你说 "I don't understand" - 那么,你需要了解电子管,因为我刚刚向你描述了 确实你的电路有问题。
1.承诺错误 - UnhandledPromiseRejectionWarning: Unhandled promise rejection...
。这不能更描述。承诺要么 1) 解决要么 2) 拒绝。如果您未能 "handle" 被拒绝的案例,您将收到错误消息。处理被拒绝的承诺可以通过以下两种方式之一进行:
// pass a 2nd function to `.then()`:
somePromise.then(function (result) {
// promise was "resolved" successfully
}, function (err) {
// Promise was "rejected"
});
// use `.catch()` - this is preferred in my opinion:
somePromise.then(function (result) {
// promise was "resolved" successfully
}).catch(function (err) {
// Promise was "rejected"
});
使用上述任一场景意味着您 "handled" 承诺拒绝正确。
2。数据库验证 - Path 'name' is required.
。这实际上意味着您有一个名为 "name" 的必需路径,它是必需的(意思是,它 必须 有一个值)。所以看看你的代码很明显:
const skill = new Skill({});
skill.save() //-> results in error
通过在保存前添加所有必需的数据来解决此问题:
const skill = new Skill({ name: "Jason" });
skill.save() //-> yay, no error
我正在尝试 return 特定的状态代码,例如 409 冲突。我用过 Model#save docs
编辑:我不是要解决错误,这是故意的。
根据文档,回调中有三个参数:err、product 和 numAffected。
编辑:我写错了这段代码并进行了编辑。不管怎样,我从 Ryan 那里得到了很好的回答。
app.post('/skill', (req, res) => {
const skill = new Skill({some_duplicate_object});
skill.save((err, product, numAffected) => {
console.log("Error: ", err);
});
不是我的 console.log,在 Mocha 测试 cli 中,我得到一个错误:
(node:19760) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 5): ValidationError: Path `name` is required.
通过玩耍和运气,我做到了:这不在 mongoose 文档中,这是我写这篇文章的主要原因 post.
app.post('/skill', (req, res) => {
const skill = new Skill({});
skill.save()
.then((err, product, numAffected) => {
console.log("Nothing displayed here");
}, (err) => {
console.log(err.errors);
});
虽然这不在文档中,但它显示了我想要的错误。作为一个真正尝试更多使用官方文档的人,我发现很难理解到底发生了什么。为什么这行得通,如果它在文档中,该信息会在哪里?
{ name:
{ MongooseError: Path `name` is required.
at ValidatorError (/home/codeamend/Coding/projects/portfolio/work/CodeAmend.Com/backend/node_modules/mongoose/lib/error/validator.js:24:11)
at validate (/home/codeamend/Coding/projects/portfolio/work/CodeAmend.Com/backend/node_modules/mongoose/lib/schematype.js:706:13)
at /home/codeamend/Coding/projects/portfolio/work/CodeAmend.Com/backend/node_modules/mongoose/lib/schematype.js:752:11
at Array.forEach (native)
at SchemaString.SchemaType.doValidate (/home/codeamend/Coding/projects/portfolio/work/CodeAmend.Com/backend/node_modules/mongoose/lib/schematype.js:712:19)
at /home/codeamend/Coding/projects/portfolio/work/CodeAmend.Com/backend/node_modules/mongoose/lib/document.js:1408:9
at _combinedTickCallback (internal/process/next_tick.js:73:7)
at process._tickCallback (internal/process/next_tick.js:104:9)
message: 'Path `name` is required.',
name: 'ValidatorError',
properties:
{ type: 'required',
message: 'Path `{PATH}` is required.',
validator: [Function],
path: 'name',
value: undefined },
kind: 'required',
path: 'name',
value: undefined,
reason: undefined } }
额外信息:
"devDependencies": {
"chai": "^3.5.0",
"mocha": "^2.4.5",
"request": "^2.81.0",
"supertest": "^3.0.0"
},
"dependencies": {
"body-parser": "^1.17.1",
"express": "^4.15.2",
"mongoose": "^4.9.2"
}
您没有处理承诺拒绝。
改变这个:
.then((err, product, numAffected) => {
console.log("Error: ", err);
});
对此:
.then((result) => {
console.log('Result:', result);
}).catch((error) => {
console.log('Error:', error);
当然可以根据需要更改回调中发生的内容。
有关未处理拒绝的更多一般信息,请参阅此答案:
你的问题是双重的,你得到的两个错误都确切地告诉你哪里出了问题。你麻烦的根源是缺乏理解(不是试图挑剔你)。这有点像你是电子学初学者,我告诉你电子管偏置不当,你说 "I don't understand" - 那么,你需要了解电子管,因为我刚刚向你描述了 确实你的电路有问题。
1.承诺错误 - UnhandledPromiseRejectionWarning: Unhandled promise rejection...
。这不能更描述。承诺要么 1) 解决要么 2) 拒绝。如果您未能 "handle" 被拒绝的案例,您将收到错误消息。处理被拒绝的承诺可以通过以下两种方式之一进行:
// pass a 2nd function to `.then()`:
somePromise.then(function (result) {
// promise was "resolved" successfully
}, function (err) {
// Promise was "rejected"
});
// use `.catch()` - this is preferred in my opinion:
somePromise.then(function (result) {
// promise was "resolved" successfully
}).catch(function (err) {
// Promise was "rejected"
});
使用上述任一场景意味着您 "handled" 承诺拒绝正确。
2。数据库验证 - Path 'name' is required.
。这实际上意味着您有一个名为 "name" 的必需路径,它是必需的(意思是,它 必须 有一个值)。所以看看你的代码很明显:
const skill = new Skill({});
skill.save() //-> results in error
通过在保存前添加所有必需的数据来解决此问题:
const skill = new Skill({ name: "Jason" });
skill.save() //-> yay, no error