Home > Back-end >  CastError: Cast to string failed for value for adding string value inside array
CastError: Cast to string failed for value for adding string value inside array

Time:11-07

Error that i am getting: *CastError: Cast to string failed for value "[ 'ZTM', 'NASA' ]" (type Array) at path "customers" at model.Query.exec (/Users/mike/Documents/NodeJS-applications/NASA-project/server/node_modules/mongoose/lib/query.js:4891:21) at model.Query.Query.then (/Users/mike/Documents/NodeJS-applications/NASA-project/server/node_modules/mongoose/lib/query.js:4990:15) at processTicksAndRejections (node:internal/process/task_queues:96:5)

{

messageFormat: undefined,

stringValue: "[ 'ZTM', 'NASA' ]",

kind: 'string',

value: [ 'ZTM', 'NASA' ],

path: 'customers',

reason: null,

valueType: 'Array'

}

I am getting this error from mongoose, and i think the problem is with customers field defined in the schema which it's type is an array of string object

Here is the code for that

import { getModelForClass, prop, Ref, index } from "@typegoose/typegoose";
import * as mongoose from "mongoose";
import { Planet } from "./planets.typegoose";

@index({ flightNumber: 1 })
class Launch {
  @prop({ type: () => Number, required: true })
  public flightNumber: number;

  @prop({ type: () => Date, required: true })
  public launchDate: Date;

  @prop({ type: () => String, required: true })
  public mission: string;

  @prop({ type: () => String, required: true })
  public rocket: string;

  @prop({ ref: () => Planet, required: true })
  public target: Ref<Planet, mongoose.Types.ObjectId>;

  @prop({ type: () => [String], required: true })
  public customers?: string[];

  @prop({ type: () => Boolean, required: true })
  public upcoming: boolean;

  @prop({ type: () => Boolean, required: true, default: true })
  public success: boolean;
}

const LaunchModel = getModelForClass(Launch);

export default LaunchModel;
     

This is where i define it's shape and type for the launch interface:

interface LaunchType {
  flightNumber?: number;
  mission: string;
  rocket: string;
  launchDate: Date;
  target: string;
  customers?: string[];
  upcoming?: boolean;
  success?: boolean;
}

Create Launch object with type annotation "LaunchType":

const launch: LaunchType = {
  flightNumber: 100,
  mission: "Kepler Exploration Soran",
  rocket: "Saturn IS2",
  launchDate: new Date("December 27, 2030"),
  target: "kepler-442 b",
  customers: ["ZTM", "NASA"],
  upcoming: true,
  success: true,
};

Function: To save this launch object to mongodb collection:

async function saveLaunchToMongoDB(launch: LaunchType): Promise<void> {
  await LaunchModel.updateOne(
    {
      flightNumber: launch.flightNumber,
    },
    launch,
    { upsert: true }
  );
}

I get no error and able to save the launch object to the MongoDB collection when i remove the property customers which it's type is string[] from the schema, interface, and the launch object

Could the problem be that property customers which is a type of string [] or undefined (union type) in the interface but in schema it returns a string constructor

CodePudding user response:

You can change the type of customers from [String] to Array.

  @prop({ type: () => Array, required: true })
 public customers? Array;
  • Related