(UPDATED)
Problem: I can't return all my testing data (as the picture shows) No data show up What I expected should be ->
{"data":[{"id": 1,"title": "title A","content": "content A","createTime": 1612708329045,"author": "Alisa"}, {"id": 2,"title": "title B","content": "content B","createTime": 1612708333855,"author": "Alisa"}],"errno":0}
Error message from terminal:
PS C:\Users\jiann\Desktop\JS\Nodejs> node www.js
internal/modules/cjs/loader.js:883
throw err;
^
Error: Cannot find module 'C:\Users\jiann\Desktop\JS\Nodejs\www.js'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:880:15)
at Function.Module._load (internal/modules/cjs/loader.js:725:27)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
at internal/main/run_main_module.js:17:47 {
code: 'MODULE_NOT_FOUND',
requireStack: []
}
I don't know where went wrong:(. Please let me know if more details are needed, appreciate your help!
My code is below:
Code Block 1 Description: If getting data and message successfully, show "errno":0, if not then show "errno":-1
class BaseModel {
constructor(data, message) {
if (typeof data === 'string') {
this.message = data
data = null
message = null
}
if (data) {
this.data = message
}
if (message) {
this.message = message
}
}
}
class SuccessModel extends BaseModel {
constructor(data, message) {
super(data, message)
this.errno = 0
}
}
class ErrorModel extends BaseModel {
constructor(data, message) {
super(data, message)
this.errno = -1
}
}
module.exports = {
SuccessModel,
ErrorModel
}
Code Block 2 Description:
set router for my blog and user. This code block will set data format in JSON, set URL path, parse query, and return 404 if no router is found.
const querystring = require('querystring')
const handleBlogRouter = require('./blog-1/src/router/blog')
const handleUserRouter = require('./blog-1/src/router/user')
const serverHandle = (req, res) => {
// set JSON
res.setHeader('Content-type', 'application/json')
//get path
const url = req.url
req.path = url.split('?')[0]
//parse query
req.query = querystring.parse(url.split('?')[0])
//set blog router
const blogData = handleBlogRouter(req, res)
if (blogData) {
res.end(
JSON.stringify(blogData)
)
return
}
//set user router
const userData = handleUserRouter(req, res)
if (userData) {
res.end(
JSON.stringify(userData)
)
return
}
//no router
res.writeHead(404, {"Content-type": "text/plain"})
res.write("404 Not Found\n")
res.end()
}
module.exports = serverHandle
Code Block 3 Description: Use GET and POST to require data from path -> '/api/blog/list' '/api/blog/detail' '/api/blog/new' '/api/blog/update' '/api/blog/del'
const { getList, getDetail } = require('../controller/blog')
const { SuccessModel, ErrorModel } = require('../model/resModel')
const handleBlogRouter = (req, res) => {
const method = req.method // GET POST
if (method === 'GET' && req.path === '/api/blog/list') {
const author = req.query.author || ''
const keyword = req.query.keyword || ''
const listData = getList(author, keyword)
return new SuccessModel(listData)
}
if (method === 'GET' && req.path === '/api/blog/detail') {
const id = req.query.id
const data = getDetail(id)
return new SuccessModel(data)
}
if (method === 'POST' && req.path === '/api/blog/new') {
return {
msg: 'dummy'
}
}
if (method === 'POST' && req.path === '/api/blog/update') {
return {
msg: 'dummy'
}
}
if (method === 'POST' && req.path === '/api/blog/del') {
return {
msg: 'dummy'
}
}
}
module.exports = handleBlogRouter)
Code Block 4 Description: These are my two testing data.
const getList = (author, keyword) => {
//fake data
return [
{
id: 1,
title: 'title A',
content: 'content A',
createTime: 1612708329045,
author: 'Alisa'
},
{
id: 2,
title: 'title B',
content: 'content B',
createTime: 1612708333855,
author: 'Amanda'
}
]
}
module.exports = {
getList
})
This error means you are accessing <?>.author
, but <?>
is undefined
.
For example, if your error is here:
if (method === 'GET' && req.path === '/api/blog/list') {
const author = req.query.author || ''
// ^^^ TypeError: Cannot read property 'author' of undefined
const keyword = req.query.keyword || ''
This means req.query
is undefined
. You can use optional chaining if there is a chance something will be present:
let foo = undefined;
foo.bar; // TypeError: Cannot read property 'bar' of undefined
foo?.bar; // => undefined
foo?.bar.baz; // TypeError: Cannot read property 'baz' of undefined
foo?.bar?.baz; // => undefined
the error is because your server hasn't started properly so all those values you are expecting will be not a available. what command did you use to start the server?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.