小编Inf*_*Dev的帖子

无法在Node.js中的ES6中定义的类中调用方法

我正在使用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)

为什么一个工作而另一个工作?救命!

javascript node.js express ecmascript-6 es6-class

10
推荐指数
2
解决办法
3797
查看次数

如果通过 Mongoose 在 Mongodb 中的另一个文档中存在引用,则有什么更好的方法可以防止文档被删除?

我正在使用以下堆栈创建一个 webapp:

  • 节点
  • 表达
  • MongoDB
  • 猫鼬

我已将应用程序构建为 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)

mongoose mongodb node.js express mongodb-query

9
推荐指数
1
解决办法
1624
查看次数

从多页面应用程序进行身份验证后,如何在初始下载期间将 JWT 令牌加载到 React?

我创建了需要身份验证的多页应用程序和单页应用程序的组合。应用程序堆栈如下。

  • Node 和 Express 用于后端和多页面应用程序
  • 用于单页应用程序的 React、React Router 和 Redux
  • 用于授权的 JWT 令牌。

我有一个无 React 登录页面,用于验证电子邮件和密码、生成 JWT 令牌并重定向到 React 应用程序。

我想在客户端首次加载 react 应用程序时将该令牌保存到 react 应用程序中。但我不知道该怎么做。如何才能做到这一点?

node.js express jwt reactjs

4
推荐指数
1
解决办法
984
查看次数

DynamoDb如何查询全局二级索引?

我用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)

node.js amazon-dynamodb

2
推荐指数
1
解决办法
5359
查看次数

如何检查属性是否只属于 Javascript 中的子类或子类?

我正在尝试获取仅存在于子类(子类)中而不存在于 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)

请帮忙!!

javascript node.js ecmascript-6

1
推荐指数
1
解决办法
283
查看次数

如何从超类创建子类的实例?

我正在创建一个类及其子类,需要在其中调用父级的静态方法以返回子级实例。

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)

javascript node.js ecmascript-6

1
推荐指数
1
解决办法
205
查看次数