Model Imported. But still company.save() not a function
I'm using .find to locate the right company and then adding values to it.....did a console.log(company)is working properly.
But when trying to save it....I get company.save() is not a function. I'm new to mongoose and programming. Can't figure out what I'm doing wrong.
const { Company } = require('../models/company');
const company = await Company.find({
userAdmin: `${data.object.metadata.userId}`
});
company[0].customerId = data.object.customer;
company[0].postNumber = data.object.metadata.postNumber;
company[0].accountStatus = data.object.metadata.accountStatus;
try {
await company.save();
} catch (ex) {
console.log(ex.message);
for (field in ex.errors) console.log(ex.errors[field].message);
}
Solution 1:
.find()
on a mongoose model returns an array of documents, not just one.
An array won't have the .find()
funtion on it; only the documents themselves inside it will.
If you're only looking for a single document, use .findOne() instead.
Solution 2:
Cause I company have multiple document in it . And you just modify one and save one too so you should try
company[0].save()