我正在使用Node.js,Express.js和MongoDB制作应用程序.我正在使用MVC模式,并且还有单独的路由文件.我正在尝试创建一个Controller类,其中一个方法调用在其中声明的另一个方法.但我似乎无法做到这一点.我得到"无法读取未定义的属性".
index.js文件
let express = require('express');
let app = express();
let productController = require('../controllers/ProductController');
app.post('/product', productController.create);
http.createServer(app).listen('3000');
Run Code Online (Sandbox Code Playgroud)
ProductController.js文件
class ProductController {
constructor(){}
create(){
console.log('Checking if the following logs:');
this.callme();
}
callme(){
console.log('yes');
}
}
module.exports = new ProductController();
Run Code Online (Sandbox Code Playgroud)
当我运行这个时,我收到以下错误消息:
Cannot read property 'callme' of undefined
Run Code Online (Sandbox Code Playgroud)
我已经运行了这个代码,只需要进行一些修改,如下所示,它可以工作.
class ProductController {
constructor(){}
create(){
console.log('Checking if the following logs:');
this.callme();
}
callme(){
console.log('yes');
}
}
let product = new ProductController();
product.create();
Run Code Online (Sandbox Code Playgroud)
为什么一个工作而另一个工作?救命!
我正在使用以下堆栈创建一个 webapp:
我已将应用程序构建为 MVC 结构。
有 Customer、OrderReceived 和 OrderSent 模式。OrderReceived 和 OrderSent 架构引用客户架构。Abridge 模式结构如下:
顾客
const mongoose = require('mongoose');
const customerSchema = mongoose.Schema({
companyName: String,
firstName: { type: String, required: true},
lastName: { type: String, required: true}
});
module.exports = mongoose.model('Customer', customerSchema);
Run Code Online (Sandbox Code Playgroud)
订单已经收到
const mongoose = require('mongoose');
const orderReceivedSchema = mongoose.Schema({
receivedDate: { type: Date, required: true},
customer: {type: mongoose.Schema.Types.ObjectId, ref: 'Customer', required: true}
});
module.exports = mongoose.model('OrderReceived', orderReceivedSchema);
Run Code Online (Sandbox Code Playgroud)
订单已发送
const mongoose = require('mongoose');
const orderSentSchema …Run Code Online (Sandbox Code Playgroud) 我创建了需要身份验证的多页应用程序和单页应用程序的组合。应用程序堆栈如下。
我有一个无 React 登录页面,用于验证电子邮件和密码、生成 JWT 令牌并重定向到 React 应用程序。
我想在客户端首次加载 react 应用程序时将该令牌保存到 react 应用程序中。但我不知道该怎么做。如何才能做到这一点?
我用Em(代表电子邮件)的Global Secondary Index创建了一个如下表。
TableName : "Users",
KeySchema: [
{ AttributeName: "Ai", KeyType: "HASH"}, //Partition key
{ AttributeName: "Ui", KeyType: "RANGE" } //Sort key
],
AttributeDefinitions: [
{ AttributeName: "Ai", AttributeType: "S" },
{ AttributeName: "Ui", AttributeType: "S" },
{ AttributeName: "Em", AttributeType: "S" }
],
GlobalSecondaryIndexes: [
{
IndexName: 'EmailIndex',
KeySchema: [
{ AttributeName: 'Em', KeyType: "HASH" },
],
Projection: {
ProjectionType: 'ALL'
},
ProvisionedThroughput: {
ReadCapacityUnits: 1,
WriteCapacityUnits: 1
}
}
],
ProvisionedThroughput: {
ReadCapacityUnits: 1,
WriteCapacityUnits: 1
} …Run Code Online (Sandbox Code Playgroud) 我正在尝试获取仅存在于子类(子类)中而不存在于 Javascript 中的父类中的属性(不包括函数)。我正在使用,.hasOwnProperty()但它也true用于父类的属性。我在 node.js 中运行它。
代码:
class Model{
constructor(){
this.location = 'Gotham'
}
}
class Superhero extends Model{
}
const superhero = new Superhero()
superhero.alias = 'Batman'
superhero.realName = 'Bruce Wayne'
for (const property in superhero){
if (superhero.hasOwnProperty(property) && (typeof superhero[property] !== 'function')){
console.log(`${property} = ${superhero[property]}`)
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
location = Gotham
alias = Batman
realName = Bruce Wayne
Run Code Online (Sandbox Code Playgroud)
我想得到的输出:
alias = Batman
realName = Bruce Wayne
Run Code Online (Sandbox Code Playgroud)
请帮忙!!
我正在创建一个类及其子类,需要在其中调用父级的静态方法以返回子级实例。
class Animal{
static findOne(){
// this has to return either an instance of Human
// or an instance of Dog according to what calls it
// How can I call new Human() or new Dog() here?
}
}
class Human extends Animal{
}
class Dog extends Animal{
}
const human = Human.findOne() //returns a Human instance
const day = Dog.findOne() //returns a Dog instance
Run Code Online (Sandbox Code Playgroud) node.js ×6
ecmascript-6 ×3
express ×3
javascript ×3
es6-class ×1
jwt ×1
mongodb ×1
mongoose ×1
reactjs ×1