This question already has answers here:
What is the correct type to use for an ObjectId field across mongoose and GraphQL?
(3 answers)
Closed 3 years ago.
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').exec()
},
Mutation: {
crearGasto: (_, input) => Gasto.create(input),
actualizarGasto: (_, input) => Gasto.findOneAndUpdate(input),
eliminarGasto: (_, { id }) => Gasto.findOneAndDelete(id)
}
};
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').exec(),
gasto: (_, { id }) => Gasto.findById(id).populate('proveedor').exec()
},
Mutation: {
crearGasto: (_, input) => Gasto.create(input),
actualizarGasto: (_, input) => Gasto.findOneAndUpdate(input),
eliminarGasto: (_, { id }) => Gasto.findOneAndDelete(id)
}
};
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.
The first argument of mongoose.model() must be the singular name of the collection your model is for. Mongoose automatically looks for the plural, lowercased version of your model name, so for your case:
mongoose.model('Gasto', gastoSchema);
mongoose.model('Proveedor', proveedorSchema);
Next you have to add a resolver for the proveedor field and .exec() to execute the mongoose queries
import Gasto from '../models/Gasto';
import Proveedor from '../models/Proveedor';
export default {
export default {
Gasto: {
proveedor: ({ proveedor }) => Proveedor.findById(proveedor).exec()
},
Query: {
gastos: () => Gasto.find().exec(),
gasto: (_, { id }) => Gasto.findById(id).exec()
},
Mutation: {
crearGasto: (_, input) => Gasto.create(input).exec(),
actualizarGasto: (_, input) => (_, { id: _id, ...input }) =>
Gasto.findOneAndUpdate({ _id }, input, { new: true }).exec(),
eliminarGasto: (_, { id: _id }) => Gasto.findOneAndDelete({ _id }).exec()
}
}
note that the resolver for actualizarGasto must be
actualizarGasto: (_, { id: _id, ...input }) => Gasto.findOneAndUpdate({ _id }, input, { new: true }).exec()
Related
This is the User schema in mongoose:
var userSchema = new mongoose.Schema({
email: {
type: String,
unique: true,
required: true,
},
name: {
type: String,
required: true,
},
Addtasks: [
{
topic: String,
words: Number,
keywords: String,
website: String,
otherdetails: String,
exampleRadios: String,
deadline: Date,
Date: String,
fileName: String,
Bigpaths: [],
},
],
});
module.exports = mongoose.model('User', userSchema);
I want to use/access the Bigpaths array, which is defined inside the Addtasks array, which is defined in User. Data is already are there in mongoDB, which I have inserted via UI page. I am trying the following code but I am getting this error in console:
data.Addtasks[Object.keys(data.Addtasks).length - 2].Bigpaths.forEach(
(element) => {
// ...
}
)
as
TypeError: Cannot read property 'Bigpaths' of undefined
at \Desktop\grumpytext\routes\index.js:99:71
Code:
const { files } = req;
User.findOne({ email: req.user.email }, function (error, data) {
if (error) {
console.log('Three');
} else if (data) {
if (Object.keys(data.Addtasks).length > 1) {
data.Addtasks[Object.keys(data.Addtasks).length - 2].Bigpaths.forEach(
(element) => {
files.forEach((currentElement) => {
if (element.name == currentElement.filename) {
files.pull(currentElement.filename);
}
});
}
);
}
}
});
How to resolve this error or how to access all the elements of Bigpaths array so that I can iterate it with forEach loop?
I'm not sure here, but I think you need to populate Addtasks prior to manipulating it:
const files = req.files;
User.findOne({email:req.user.email}).populate('Addtasks').exec((error, data) => {
if (error) {
console.log("Three");
}
else
{
if(data)
{
if(Object.keys(data.Addtasks).length > 1)
{
console.log("Addtasks count: " + Object.keys(data.Addtasks).length);
data.Addtasks[Object.keys(data.Addtasks).length - 2].Bigpaths.forEach(element => {
files.forEach(currentElement => {
if(element.name == currentElement.filename)
{
files.pull(currentElement.filename);
}
})
});
}
}
}
});
Please notice the log console.log("Addtasks count: " + Object.keys(data.Addtasks).length); - in case the solution does not work, I advise to add some prints, especially to check if the count of elements is as expected or properties within an object are fine.
In prisma 1 I have used fragment to fetch the nested fields.
For example:
const mutations = {
async createPost(_, args, ctx) {
const user = await loginChecker(ctx);
const post = await prisma.post
.create({
data: {
author: {
connect: {
id: user.id,
},
},
title: args.title,
body: args.body,
published: args.published,
},
})
.$fragment(fragment);
return post;
},
};
but seems like in prisma2 it is not supported. because by running this on playground,
mutation CREATEPOST {
createPost(
title: "How to sleep?"
body: "Eat, sleep, repaet"
published: true
) {
title
body
published
author {
id
}
}
}
I am getting,
"prisma.post.create(...).$fragment is not a function",
The include option is used to eagerly load relations in Prisma.
Example from docs:
const result = await prisma.user.findOne({
where: { id: 1 },
include: { posts: true },
})
Assuming a user table with a one-to-many posts relation, this will return back the user object with the posts field as well.
Prisma also supports nesting as well, for example:
const result = await prisma.user.findOne({
where: { id: 1 },
include: {
posts: {
include: {
author: true,
}
},
},
})
The project is created with nodejs and mongoose. What I am trying to do is to update the existing model with addition data (which is a comment, in that case).
This is the model and its methods:
const bugSchema = new Schema({
title: {
type: String,
required: true
},
description: {
type: String,
required: true
},
date: {
type: String,
required: true
},
time: {
type: String,
required: true
},
assignedTo: {
type: String,
required: true
},
assignedBy: {
type: String,
required: true
},
status: {
type: String,
required: true
},
priority: {
type: String,
required: true
},
comments: {
comment:[
{
user:{
type: String,
required: true
},
content: {
type: String,
required: true
}
}
]
}
});
bugSchema.methods.addComment = function(comment){
const username = comment.user;
const content = comment.content;
console.log(comment);
const updatedComments = [...this.comments];
updatedComments.push({
user : username,
content: content
});
this.comments = updatedComments;
return this.save();
};
The controller, which is passing the information from the form:
exports.postComment = (req,res,next) =>{
const bugId = req.body.bugID;
const name = req.session.user.fullName;
const content = req.body.content;
const prod = {name, content};
Bug.findById(bugId).then(bug =>{
return bug.addComment(prod);
})
.then(result =>{
console.log(result);
});
};
I am getting a following error:
(node:3508) UnhandledPromiseRejectionWarning: TypeError: this.comments is not iterable
(node:3508) UnhandledPromiseRejectionWarning: TypeError: this.comments is not iterable
The error indicate you're trying to iterable a type of data which does NOT has that capability.
You can check that printing the type:
console.log(typeof this.comments)
Or even, priting the whole object:
console.log(this.comments)
as you can see, in both cases you're getting an object, not a list (how you spect)
So you can do 2 things:
1- Iterable a list
this.comments is an object but into that object you have the list you want, so just use the list instead.
bugSchema.methods.addComment = function(comment){
const username = comment.user;
const content = comment.content;
console.log(comment);
//const updatedComments = [...this.comments];
const updatedComments = [...this.comments.comment];
updatedComments.push({
user : username,
content: content
});
this.comments = updatedComments;
return this.save();
};
Or you can modify your schema making the comments a list instead of an object
2- comments as list in schema
Define the comments attribute as a list
const bugSchema = new Schema({
title: {
type: String,
required: true
},
description: {
type: String,
required: true
},
...
...,
comments:[
{
user:{
type: String,
required: true
},
content: {
type: String,
required: true
}
}
]
});
And then, try to iterable it as how you been doing
bugSchema.methods.addComment = function(comment){
const username = comment.user;
const content = comment.content;
console.log(comment);
const updatedComments = [...this.comments];
updatedComments.push({
user : username,
content: content
});
this.comments = updatedComments;
return this.save();
};
I am not sure but comments is an object and not an array so you can't push using [...this.comments] and I think it is the comment you want to push?
const updatedComments = [...this.comment];
updatedComments.push({
user : username,
content: content
});
this.comment = updatedComments;
From your schema comments is not an array. you are trying to spread an object into an array. const updatedComments = [...this.comments]; also push works on array.
try to modify your schema definitions by declaring the commentSchema outside the bugSchema.
const commentSchema = new Schema({
user:{
type: String,
required: true
},
content: {
type: String,
required: true
}
})
const bugSchema = new Schema({
comments: {
type: [commentSchema]
}
})
Bug.findByIdAndUpdate(bugId, {$push: {comments: newComment}})
Don't use findByIdAndUpdate Mongoose method, you better use save
it is written here https://mongoosejs.com/docs/tutorials/findoneandupdate.html
The findOneAndUpdate() function in Mongoose has a wide variety of use cases. You should use save() to update documents where possible, but there are some cases where you need to use findOneAndUpdate(). In this tutorial, you'll see how to use findOneAndUpdate(), and learn when you need to use it.
Below a router example
router.put('/items', (req, res) => {
if (!req.body._id || !req.body.title) {
return res.status(501).send({ message: 'Missing parameters, or incorrect parameters' });
}
return itemModel.findOne({ _id: req.body._id }, (err, item) => {
if (err) {
return res.status(500).send({
message: err
});
}
item.title = req.body.title; // <------------- You rewrite what was before stored on title attribute
return item.save((err, item) => { // <------------- You save it, this is not gonna create a new one, except if it doesn't exist already
if (err) {
return res.status(400).send({
message: 'Failed to update item'
});
} else {
return res.status(200).send({
message: 'Item update succesfully',
data: item
});
}
});
});
});
How to properly use nested include query with sequelize(nodejs)
my nested include query with sequelize is giving me this error
"category is associated to course using an alias. You've included an alias (Category), but it does not match the alias defined in your association."
what i want is from the users list, i want to get all the tcourses, then from that, i will get all the courses then from that i want to get the list of all the categories
I don't know what I did that is wrong, I just need it to work, am still new to nodejs
here is the code
export default {
Query: {
users: combineResolvers(isAdmin, async (parent, args, { models }) => {
return await models.User.findAll();
}),
},
User: {
messages: async (user, args, { models }) => {
return await models.Message.findAll({
where: { userId: user.id },
});
},
tcourses: async (user, args, { models }) => {
return await models.TCourses.findAll({
where: { UserId: user.id },
include: [{model: models.Course, as: 'Course', include: [{model: models.Category, as: 'Category'}]}]
})
}
},
}
here is my tcourses model code
const tcourses = (sequelize, DataTypes) => {
const TCourses = sequelize.define('tcourses', {
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
unique: true,
primaryKey: true,
field: 'id'
},
}, {
timestamps: false
});
TCourses.associate = models => {
TCourses.belongsTo(models.Course,{ as: 'Course', required: false})
TCourses.belongsTo(models.User,{ as: 'User'})
};
return TCourses;
};
export default tcourses;
here is my Course model code
const course = (sequelize, DataTypes) => {
const Course = sequelize.define('course', {
Title: {
type: DataTypes.STRING,
validate: {
notEmpty: {
args: true,
msg: 'Enter Course Title'
}
},
},
}, {
timestamps: false
})
Course.associate = models => {
Course.belongsTo(models.Category, { as: 'Cat'})
Course.hasMany(models.Requirements,{onDelete: 'CASCADE', foreignKey: 'CourseId'})
Course.belongsTo(models.User, { as: 'Owners'})
Course.hasMany(models.Audience,{onDelete: 'CASCADE', foreignKey: 'CourseId'})
Course.hasMany(models.TCourses,{onDelete: 'CASCADE', foreignKey: 'CourseId'})
};
return Course
};
export default course
here is my Category model code
const category = (sequelize, DataTypes) => {
const Category = sequelize.define('category', {
catName: {
type: DataTypes.STRING,
validate: {
notEmpty: {
args: true,
msg: 'Enter Category name'
}
},
},
}, {
timestamps: false
});
Category.associate = models => {
Category.hasMany(models.Course,{onDelete: 'CASCADE', foreignKey: 'CatId'})
Category.hasMany(models.TCategory,{onDelete: 'CASCADE', foreignKey: 'CategoryId'})
};
return Category;
};
export default category;
pls how can i make it to work
You have to provide the same alias for model in both include statement and while defining association.
The error also specifies the same,
"category is associated to course using an alias. You've included an alias (Category), but it does not match the alias defined in your association."
While querying the DB you have specified the alias for category table as Category
models.TCourses.findAll({
where: { UserId: user.id },
include: [{model: models.Course, as: 'Course', include: [{model: models.Category, as: 'Category'}]}]
})
In the Course model you have defined the association as Cat
Course.belongsTo(models.Category, { as: 'Cat'})
Now you have to make it same, i,e use either Cat or Catgory at both the places.
I'm trying to correctly type a function that uses Flow generic (or polymorphic) types to paginate an array of objects into a graphql/Relay-style "page" object.
Here's a simplified example:
type DBNode = {
uid: string
}
type Student = DBNode & {
username: string
}
const students = [{
uid: '123',
username: 'user-one',
}, {
uid: '345',
username: 'user-two',
}]
type Classroom = DBNode & {
room: number
}
const classrooms = [{
uid: '234',
room: 666,
}, {
uid: '456',
room: 667,
}]
type Edge<T> = {
cursor: string,
node: T
}
const itemToEdge = <T>(item: T): Edge<T> => ({
cursor: item.uid,
node: item
})
type Page<T> = {
pageInfo: {
count: number,
},
edges: Array<Edge<T>>
}
const assemblePage = <T>(items: Array<T>): Page<T> => ({
pageInfo: {
count: items.length,
},
edges: items.map(itemToEdge)
})
const studentPage = (): Page<Student> => {
return assemblePage(students)
}
const classroomPage = (): Page<Classroom> => {
return assemblePage(classrooms)
}
The error I receive is:
cursor: item.uid,
^ Cannot get `item.uid` because property `uid` is missing in `T` [1].
References:
34: const itemToEdge = <T>(item: T): Edge<T> => ({
^ [1]
Is there a to ensure that the polymorphic type T is always an object that has a uid field?
Alternatively, I tried typing assemblePage and itemToEdge to require that the item(s) are of a DBNode type, which results in:
38: node: item ^ Cannot return object literal because `DBNode` [1] is incompatible with `T` [2] in property `node`.
References:
35: const itemToEdge = <T>(item: DBNode): Edge<T> => ({
^ [1]
31: node: T ^ [2]
Demo
Edit: Here's a working version of the demo, thanks #Aleksey L for the help!
Working Demo
You can define constraint to generic type parameter:
const itemToEdge = <T: DBNode>(item: T): Edge<T> => ({
cursor: item.uid,
node: item
})
const assemblePage = <T: DBNode>(items: Array<T>): Page<T> => ({
...
})
From docs:
Generics allow you to hold onto the more specific type while adding a constraint