У меня есть схема производителя в mongoose с некоторыми данными, хранящимися в mongo.Моя цель - иметь возможность редактировать название производителя через форму.Но я получаю следующую ошибку при изменении имени и нажатии «Отправить».
Проверка производителя не удалась: _id: Преобразование в строку не выполнено для значения "['example'," example edited '] "at path"_id "
Я понимаю, что существует какой-то конфликт с _id, так как он автоматически создается Монго.Но в этом случае я должен использовать _id для имени объекта.
Вот моя схема Мангуста
var manufacturerSchema = mongoose.Schema({
// The _id property serves as the primary key. If you don't include it
// the it is added automatically. However, you can add it in if you
// want to manually include it when creating an object.
// _id property is created by default when data is inserted.
_id: {"type" : String},
mfgDiscount: {"type" : Number}
},
{ // Include this to eliminate the __v attribute which otherwise gets added
// by default.
versionKey: false
});
Вот обновление
async update(editedObj) {
// Load the corresponding object in the database.
let manufacturerObj = await this.getManufacturer(editedObj.id);
// Check if manufacturer exists.
if(manufacturerObj) {
// Manufacturer exists so update it.
let updated = await Manufacturer.updateOne(
// Set new attribute values here.
{$set: { _id: editedObj._id }});
// No errors during update.
if(updated.nModified!=0) {
response.obj = editedObj;
return response;
}
Функция обновления
// Receives posted data that is used to update the item.
exports.Update = async function(request, response) {
let manufacturerID = request.body._id;
console.log("The posted manufacturer id is: " + manufacturerID);
// Parcel up data in a 'Manufacturer' object.
let tempManufacturerObj = new Manufacturer( {
_id: manufacturerID,
id: request.body._id,
});
// Call update() function in repository with the object.
let responseObject = await _manufacturerRepo.update(tempManufacturerObj);
// Update was successful. Show detail page with updated object.
if(responseObject.errorMessage == "") {
response.render('Manufacturer/Detail', { manufacturer:responseObject.obj,
errorMessage:"" });
}
// Update not successful. Show edit form again.
else {
response.render('Manufacturer/Edit', {
manufacturer: responseObject.obj,
errorMessage: responseObject.errorMessage });
}
};
и вот форма
<% layout('../layouts/mainlayout.ejs') -%>
<form action="/Manufacturer/Update" method="POST">
<input type='hidden' name="_id" value= <%= manufacturer._id %> /> <br/>
<input type='text' name="_id"
value="<%= manufacturer._id ? manufacturer._id : '' %>" /><br/>
<input type="submit" value="Submit">
</form>
<%= errorMessage %>