Home > Blockchain >  i can't a create a model in NodeJS
i can't a create a model in NodeJS

Time:01-25

when i try to create a model in NodeJS

const mongoose = require ('mongoose');

const User = mongoose.model('User',{ name:{ type:string }, lastname:{ type:string }, age: { type:Number }

})

module.exports = User;

**errorIS ** C:\Users\models\user.js:6 type:string ^ ReferenceError: string is not defined at Object.<anonymous> (C:\Users\moham\Desktop\backend\models\user.js:6:14) at Module._compile (node:internal/modules/cjs/loader:1159:14) at Module._extensions..js (node:internal/modules/cjs/loader:1213:10) at Module.load (node:internal/modules/cjs/loader:1037:32) at Module._load (node:internal/modules/cjs/loader:878:12) at Module.require (node:internal/modules/cjs/loader:1061:19) at require (node:internal/modules/cjs/helpers:103:18) at Object.<anonymous> (C:\Users\moham\Desktop\backend\server.js:2:14) at Module._compile (node:internal/modules/cjs/loader:1159:14) at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)

Node.js v18.12.1

CodePudding user response:

The error message is saying that the variable "string" is not defined. In Mongoose, when creating a model, the data types for the fields are typically specified as strings, not by using the JavaScript keyword "string". To fix this, you can change the following line:

name:{ type:string }, lastname:{ type:string }

to:

name:{ type:String }, lastname:{ type:String }

This should resolve the "ReferenceError: string is not defined" error.

  • Related