【问题标题】:Cannot use mongoosastic functions .x is not a function不能使用 mongoosastic 函数 .x 不是函数
【发布时间】:2020-03-22 07:55:37
【问题描述】:

我尝试使用 npm 添加 mongoosastic @types 但这不起作用,我怎样才能让它识别这些功能? (.search() 也不起作用)

VS Code 描述的错误:

模块“c:/Users/joche/Desktop/Proyectos/E-Commerce/Backend/node_modules/mongoosastic/lib/mongoosastic” 找不到模块“mongoosastic”的声明文件。 'c:/Users/joche/Desktop/Proyectos/E-Commerce/Backend/node_modules/mongoosastic/lib/mongoosastic.js' 隐含了一个 'any' 类型。 尝试npm install @types/mongoosastic(如果存在)或添加包含declare module 'mongoosastic';ts(7016) 的新声明(.d.ts)文件

这是我的代码:

const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const mongoosastic = require("mongoosastic");
const Schema = mongoose.Schema;
const app = express();

app.use(bodyParser.urlencoded({
    extended: true
}));

app.use(express.static('public'));


mongoose.connect("mongodb://localhost:27017/gaminghard", {useNewUrlParser: true});

const ProductSchema = mongoose.Schema({
    _id: mongoose.Schema.Types.ObjectId,
    name: { type:String, es_indexed:true },
    category: String,
    brand: String,
    model: { type:String, es_indexed:true },
    desc: { type:String, es_indexed:true },
    price: Number,
    stock: Number,
    weight: Number,
    img: String,
    quantityToBuy: Number
},
{
    collection: 'Products'
});

const Product = mongoose.model("Product", ProductSchema);

ProductSchema.plugin(mongoosastic, {
    hosts: [
        "localhost:9200"
    ]
});

Product.createMapping(function(err, mapping){
    if(err){
        console.log("error creating mapping");
        console.log("err");
    }else{
        console.log("Mapping successfully created");
        console.log(mapping);
    }
});

app.listen(3000, function(){
    console.log("Server started listening on port 3000");
});

Nodemon 错误:

C:\Users\joche\Desktop\Proyectos\E-Commerce\Backend\server.js:60
Product.createMapping(function(err, mapping){
        ^

TypeError: Product.createMapping is not a function
    at Object.<anonymous> (C:\Users\joche\Desktop\Proyectos\E-Commerce\Backend\server.js:60:9)
    at Module._compile (internal/modules/cjs/loader.js:959:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)
    at Module.load (internal/modules/cjs/loader.js:815:32)
    at Function.Module._load (internal/modules/cjs/loader.js:727:14)
    at Function.Module.runMain (internal/modules/cjs/loader.js:1047:10)
    at internal/main/run_main_module.js:17:11
[nodemon] app crashed - waiting for file changes before starting...

package.json:

{
  "name": "Backend",
  "version": "1.0.0",
  "description": "",
  "main": "server.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node server.js"
  },
  "keywords": [],
  "author": "Jose Ignacio Carbone",
  "license": "No license - private",
  "dependencies": {
    "@types/express": "^4.17.2",
    "@types/mongoose": "^5.5.32",
    "express": "^4.17.1",
    "mongoosastic": "^4.5.1",
    "mongoose": "^5.7.12",
  }
}

编辑:尝试添加 .d.ts 文件,VS Code 错误消失但仍无法编译源代码。

【问题讨论】:

    标签: node.js mongoose mongoosastic


    【解决方案1】:

    这里自己回答,不得不搬家

    ProductSchema.plugin(mongoosastic, { 主持人:[ “本地主机:9200” ] });

    以上

    const Product = mongoose.model("Product", ProductSchema);

    因为我随后在没有应用插件的情况下使用 Product,这没有意义。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-06
      • 1970-01-01
      • 2020-10-10
      • 2020-04-17
      • 2019-01-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多