如果我通过 Model.getById 以外的自定义索引检索文档,我似乎无法更新它:
let UserModel = ottoman.model("User", {
createdON: {type: "Date", default:function(){return new Date()}},
name: {
first: "string",
last: "string"
},
address: {
street: "string",
city: "string",
state: "string",
zip: "integer",
country: {type: "string", default: "Somewhere"}
},
phone: "string",
email: "string",
email_verified: {type: "boolean", default: false},
password: "string",
active: {type: "boolean", default: true}
}, {
index: {
findByEmail: {
by: "email",
type: "refdoc"
},
findByLastName: {
by: "name.last",
type: "n1ql"
}
}
});
ottoman.ensureIndices((error) => {
if (error) {
return console.error("Error ensuring indices on User", error);
}
//console.log("Ensured indices on User");
});
这工作正常:
await UserModel.getById(request.params.token, (error, user) => {
if (error) {
console.error('UserController getById error: ', error);
return response.status(400).send({"status": "error", "message": "User not found"});
}
// Set verification flag
user.email_verified = true;
// Persist user document
user.save((error, result) => {
if (error) {
console.log('Error updating user: ', error);
throw new Error(error);
}
console.log('Updated user: ', user._id);
response.status(200).send({"status": user, "message": "Email verified"});
});
});
这不会:
await UserModel.find({email: 'tester@testing.com'}, (error, user) => {
if (error) {
console.error('UserController getById error: ', error);
return response.status(400).send({"status": "error", "message": "User not found"});
}
// Set verification flag
user.active = true;
// Persist user document
user.save((error, result) => {
if (error) {
console.log('Error updating user: ', error);
throw new Error(error);
}
console.log('Updated user: ', user._id);
response.status(200).send({"status": user, "message": "Email verified"});
});
});
错误:
TypeError: user.save is not a function
at UserModel.find (/Users/something/something/something/something/something/something.js:291:22)
at handler (/Users/something/something/something/node_modules/ottoman/lib/ottoman.js:626:9)
at loadSubItem (/Users/something/something/something/node_modules/ottoman/lib/modelinstance.js:587:7)
at /Users/something/something/something/node_modules/ottoman/lib/modelinstance.js:635:9
at /Users/something/something/something/node_modules/ottoman/lib/cbstoreadapter.js:77:5
我在更新用户时做错了吗?我已经尝试过使用多种文档/模型类型,结果相同。