I have created a moleculer project as below:
https://codesandbox.io/s/8d5xy3
now I changed default database to mongodb/mongoose:
but when I run products.service.js
's action: createProduct
, I get 500 error:
{
"name": "MoleculerError",
"message": "Could not create product",
"code": 500,
"type": "CREATE_PRODUCT_ERR",
"data": {
"error": "Cannot read properties of undefined (reading 'insert')"
}
}
action code:
createProduct: {
rest: {
method: "PUT",
path: "/product"
},
params: {
name: "string",
price: "number",
quantity: "number",
},
async handler(ctx) {
const { name, price, quantity } = ctx.params;
try {
console.log({name, price, quantity}, this.adapter)
const product = await this.adapter.insert({ name, price, quantity });
return product;
} catch (err) {
throw new MoleculerError("Could not create product", 500, "CREATE_PRODUCT_ERR", { error: err.message });
}
}
},
After debugging, I find the reason: this.adapter
is undefined
.
I don't know how to solve this problem, please help.