با یه تیر دو نشان بزن🎯، هدیه 6 میلیونی(متخصص لینوکس) به همراه ۲0٪ تخفیف روی همه دوره‌های متخصص😍
۰ ثانیه
۰ دقیقه
۰ ساعت
۴ هادی جیبر
عدم شناسایی متد get
جامعه نود جی اس ایجاد شده در ۱۷ مهر ۱۴۰۳

با سلام

در فایل routes\conatcts.js کد به شرح ذیل نوشته شده :

import express from 'express';

import { Router } from 'express';

import { getcontacts , createcontact , deleteContact , updateContact } from '../controller/contacts.js';

const contactrouter = await express.Router();

contactrouter.get('/list' , getcontacts);

contactrouter.post('/new' , createcontact);

contactrouter.deleteContact('/:id' , deleteContact);

contactrouter.put('/:id' , updateContact);

 

export default contactrouter;

 

بعد از اجرا با خطای زیر مواجه می‌شوم و متد get را نمی‌شناسد :

contactrouter.get('/list' , getcontacts);
             ^

TypeError: Cannot read properties of undefined (reading 'get')

 

ممکنه راهنمایی بفرمایید

سلام،

express.Router() یک تابع معمولیه و نیازی به async/await ندارد.

ضمناً contactrouter.deleteContact هم باید تبدیل به contactrouter.delete شود.

ایپورت دوم هم ضروری نیست.

import express from 'express';
import { getcontacts, createcontact, deleteContact, updateContact } from '../controller/contacts.js';
const contactrouter = express.Router();
contactrouter.get('/list', getcontacts);
contactrouter.post('/new', createcontact);
contactrouter.delete('/:id', deleteContact);
contactrouter.put('/:id', updateContact);
export default contactrouter;
محسن موحد ۱۷ مهر ۱۴۰۳، ۲۱:۳۰

با سلام و تشکر از راهنمایی شما

مشکل من با این تغییرات حل نشد

حتی روتر جدید به نام a تعریف و تست کردم و هنوز متد get را نمیشناسه

 

a.get('/list' , getcontacts);
 ^

TypeError: Cannot read properties of undefined (reading 'get')

هادی جیبر ۱۸ مهر ۱۴۰۳، ۰۵:۴۷

پروژتون رو قرار بدید تست کنم.

محسن موحد ۱۹ مهر ۱۴۰۳، ۱۱:۵۲

با سلام و تشکر

import express from 'express';

import { getcontacts , createcontact , deleteContact , updateContact } from '../controller/contacts.js';

const contactrouter =  express.Router();

const a = express.Router();

a.get('/list' , getcontacts);

export default a;

 

پیغام خطای زیر را میدهد :

file:///D:/7Learn%20NodeJS%20Backend%20Expert/NodejsApp/ContactListSequelize/src/server/routes/contacts.js:7
a.get('/list' , getcontacts);
 ^

TypeError: Cannot read properties of undefined (reading 'get')

ممنون میشم راهنمایی بفرمایید

هادی جیبر ۲۱ مهر ۱۴۰۳، ۰۹:۴۵