Home > front end >  cant save data after push to a array in acollection
cant save data after push to a array in acollection

Time:10-29

mongoose function .save() is not working .

async function insertContainerizedData(data, id, farmerId) {
    console.log('test id: ', new ObjectId(id));
    const user = await Storage.findOne({'containerizedBatches.storage_id': new ObjectId(id)});
    console.log('userrrr', user);
    if (user == null) {
        const containerizedBatched = await Storage.updateOne({'farmer_id': farmerId}, {
            $push: {
                containerizedBatches: {
                    storage_id: new ObjectId(data.storage_id),
                    trace_id: data.trace_id,
                    batch_id: data.batch_id,
                    actual_quantity: data.actual_quantity,
                    remaining_quantity: data.remaining_quantity,
                    actual_container: data.actual_container,
                    remaining_container: data.remaining_container,
                    container_type: data.container_type,
                    grade: data.grade
                }
            }
        });
        const result = await containerizedBatched.save(async (err, result) => {
            if (err) {
                throw new Error('item is not saved');
            } else {
                return result;
            }
        });
        console.log('result after push', result);
        return result;

its my code and the output is as bellow in postman What iam trying to do is if the user == null i need to push all the data(req.body) into a array as an object and name of the array is containerizedBatches.

  "debugMessage": "containerizedBatched.save is not a function",

CodePudding user response:

You don't need to save after you have already run the update. containerizedBatched is the result of your update operation, not a model to save.

  • Related