Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 138163

Mongoose + GraphQL cannot populate model

$
0
0

Well, I know there are a lot of questions regarding this issue but neither of their solutions worked for me, or I can't find what is happening.

I have a backend server running Apollo Server + Mongoose on localhost. My issue is I can't populate or create the collection since I get:

Error: "ID cannot represent value: <Buffer 5e 38 65 18 f1 e3 f5 43 10 d4 c1 45>".

This is the model:

import mongoose from 'mongoose';
const Float = require('mongoose-float').loadType(mongoose);

const gastoSchema = new mongoose.Schema({
    descripcion: String,
    importe: Float,
    proveedor: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Proveedor'
    },
}, {
    timestamps: {
        createdAt: 'fechaCreacion',
        updatedAt: 'fechaActualizacion'
    }
});

export default mongoose.model('gastos', gastoSchema);

This is my type definition:

import { gql } from 'apollo-server';

export default gql`
    type Gasto {
        id: ID
        descripcion: String
        importe: Float
        proveedor: Proveedor
    }

    extend type Query {
        gastos: [Gasto]
        gasto(id: ID!): Gasto
    }

    extend type Mutation {
        crearGasto(descripcion: String, importe: Float, proveedor: ID): Gasto
        actualizarGasto(id: ID!, descripcion: String, importe: Float, proveedor: ID): Gasto
        eliminarGasto(id: ID!): String
    }
`;

And this is my resolver:

import Gasto from '../models/Gasto';

export default {
    Query: {
        gastos: () => Gasto.find().populate('proveedores').exec(),
        gasto: (_, { id }) => Gasto.findById(id).populate('proveedores')
    },
    Mutation: {
        crearGasto: (_, input) => Gasto.create(input),
        actualizarGasto: (_, input) => Gasto.findOneAndUpdate(input),
        eliminarGasto: (_, { id }) => Gasto.findOneAndDelete(id).exec()
    }
};

Attempt 1

I have tried this change on my model definition but it didn't work:

import mongoose from 'mongoose';
const Float = require('mongoose-float').loadType(mongoose);
const ObjectId = require('mongoose').Types.ObjectId;

ObjectId.prototype.valueOf = function() { return this.toString(); }

const gastoSchema = new mongoose.Schema({
    descripcion: String,
    importe: Float,
    proveedor: {
        type: ObjectId,
        ref: 'Proveedor'
    },
}, {
    timestamps: {
        createdAt: 'fechaCreacion',
        updatedAt: 'fechaActualizacion'
    }
});

export default mongoose.model('gastos', gastoSchema);

Attempt 2

According to the mongoose documentation I also tried switching between populate('proveedores') (plural), populate('proveedor') (singular) but the error changes:

import Gasto from '../models/Gasto';

export default {
    Query: {
        gastos: () => Gasto.find({}).populate('proveedor'),
        gasto: (_, { id }) => Gasto.findById(id).populate('proveedor')
    },
    Mutation: {
        crearGasto: (_, input) => Gasto.create(input),
        actualizarGasto: (_, input) => Gasto.findOneAndUpdate(input),
        eliminarGasto: (_, { id }) => Gasto.findOneAndDelete(id).exec()
    }
};

Error: "message": "Schema hasn't been registered for model \"Proveedor\".\nUse mongoose.model(name, schema)".

Just in case this is my Proveedor model definition:

import mongoose from 'mongoose';

const proveedorSchema = new mongoose.Schema({
    nombre: {
        type: String,
        required: true
    },
    telefono: String,
    direccion: String,
    email: String,
}, {
    timestamps: {
        createdAt: 'fechaCreacion',
        updatedAt: 'fechaActualizacion'
    }
});

export default mongoose.model('proveedores', proveedorSchema);

This is what i am querying on the GraphQL playground:

query {
  gastos {
    id
    importe
    proveedor {
      id
      nombre
    }
  }
}

Any clues? Thanks in advance.


Viewing all articles
Browse latest Browse all 138163

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>