gpt4 book ai didi

node.js - 类型错误 : undefined is not a function - node. js

转载 作者:太空宇宙 更新时间:2023-11-04 00:06:20 24 4
gpt4 key购买 nike

我以前见过这个问题,但还没有找到答案。我的函数定义得很清楚,我什至在调用它之前放置了一个断点,并检查了它的“typeof”。

Basically, I'm importing a utilites.js file to validate a result after find, and that validation function is called by various functions.  For some it works, for others it throws an error.  Can someone explain to me what's going on?

//这是包含使用它的函数的文件:(下面我将添加实用程序文件)

    const mongoose = require('mongoose');
const moment = require('moment');
const utilities = require('./utilities');
const models = require('../models/schemas');
const hash = require('object-hash');
const { Beverage, Purchase, Machine } = models;
const { ValidatedAfterFind, ValidatedAfterUpdate } = utilities;
const blueBirdPromise = require('bluebird');

mongoose.Promise = require('bluebird');

//THE VALIDATION FUNCTION WORKS HERE
const FetchBeveragePrice = (req, res) => {
const { beverageid } = req.params;
let beverage = '';
return Beverage.find({ _id: beverageid }).select('Price').exec()
.then(returnedBeverage => {
beverage = returnedBeverage[0]._doc;
if(ValidatedAfterFind(returnedBeverage, beverageid)) {
res.send({Price:beverage.Price})
}
else {
res.sendStatus(500)
}
})
}


const AddCoins = (req, res) => {

const { machineId, coinType } = req.body;
if (wrongCoinType(coinType)) {
return res.status(400).send("Wrong Coin Type")
}
Machine.updateOne({ _id: machineId }, { $inc: { fundsForCurrentPurchase: coinType, totalFunds: coinType } }).exec()
.then(results => {
if(ValidatedAfterUpdate(results, 1)){
res.sendStatus(200);
}
else {
res.sendStatus(500);
}
})

}
//THE VALIDATION FUNCTION DOESN'T WORK HERE
const GetBeverage = (req, res) => {
const { machineid, beverageid } = req.params;

return blueBirdPromise.join(Beverage.findById({ _id: beverageid }).exec(), Machine.find({ _id: machineid }).exec(),
(beverage, machine) => {
if (ValidatedAfterFind(beverage, beverageid) && ValidatedAfterFind(machine, machineid)) {
choosePurchaseProcessByConditions (machine, beverage, res)
}
else {
res.sendStatus(500)
}

})
}

function choosePurchaseProcessByConditions (machine, beverage, res) {
if (machine.beveragesInMachine[beverageid.toString()] > 0) {
if (machine.fundsForCurrentPurchase >= beverage.Price || machine.currentlyApprovedCreditCard) {
chooseStepsBeforePurchaseByBeverageType(machine, beverage, res);
}
else {
res.send(`Missing ${beverage.Price - machine.fundsForCurrentPurchase} shekels`);
}
}
else {
return res.send('Out of Stock');
}
}

function chooseStepsBeforePurchaseByBeverageType(machine, beverage, res) {
if (beverage.hot) {
if (machine.sugarUpdated) {
addPurchaseAndUpdateMachine(res, machine, beverage)
}
else {
return res.send("Choose Amount of Sugar")
}
}
else {
addPurchaseAndUpdateMachine(res, machine, beverage)
}
}

function addPurchaseAndUpdateMachine(res, machine, beverage) {
const newPurchase = new Purchase({ machineID: machine._id, creditCard: machine.currentlyApprovedCreditCard, price, creationDate: moment() });
const change = machine.fundsForCurrentPurchase - beverage.Price;
const updateBeveragesInMachine = `beveragesInMachine.${beverage._id.toString()}`;

const updateFieldsForMachine = {
$inc: {
totalFunds: -(machine.totalFunds - beverage.Price),
updateBeveragesInMachine: -1
},
fundsForCurrentPurchase: 0,
currentBeverage: '',
currentlyApprovedCreditCard: false,
sugarUpdated: false
}
return blueBirdPromise.join(newPurchase.save(), Machine.updateOne({ _id: machineid }, updateFieldsForMachine),
(purchaseResult, machineResult) => {
return blueBirdPromise.join(utilities.ValidationPromiseOnUpdate(purchaseResult, 1), utilities.ValidationPromiseOnUpdate(machineResult, 1),
() => res.json({ change }))
.catch(() => res.sendStatus(500))
})
}


module.exports = {
FetchBeveragePrice,
AddCoins,
GetBeverage
}


Here is the file with the validation functions:



const ValidatedAfterFind = ([result], searchedId) => result._doc._id.toString() === searchedId.toString();

const ValidatedAfterUpdate = (results, expectedModifications) => results.nModified === expectedModifications;


module.exports = {
ValidatedAfterFind,
ValidatedAfterUpdate
}

这让我快疯了,找不到答案!

我认为这可能与之前的循环引用有关,但事实并非如此。

任何帮助将不胜感激。

提前致谢。

堆栈跟踪:未处理的拒绝类型错误:未定义不是函数 在 ValidatedAfterFind (c:\Users\Amir\Desktop\vending_machines\dal\utilities.js:16:28) 在 Beverage.findById.then.beverage (c:\Users\Amir\Desktop\vending_machines\dal\data.js:63:16) 在 tryCatcher (c:\Users\Amir\Desktop\vending_machines\node_modules\bluebird\js\release\util.js:16:23) 在 Promise.module.exports.Promise._settlePromiseFromHandler (c:\Users\Amir\Desktop\vending_machines\node_modules\bluebird\js\release\promise.js:512:31) 在 Promise.module.exports.Promise._settlePromise (c:\Users\Amir\Desktop\vending_machines\node_modules\bluebird\js\release\promise.js:569:18) 在 Promise.module.exports.Promise._settlePromise0 (c:\Users\Amir\Desktop\vending_machines\node_modules\bluebird\js\release\promise.js:614:10) 在 Promise.module.exports.Promise._settlePromises (c:\Users\Amir\Desktop\vending_machines\node_modules\bluebird\js\release\promise.js:693:18) 在 Async._drainQueue (c:\Users\Amir\Desktop\vending_machines\node_modules\bluebird\js\release\async.js:133:16) 在 Async._drainQueues (c:\Users\Amir\Desktop\vending_machines\node_modules\bluebird\js\release\async.js:143:10) 在 Immediate.e.Async.drainQueues (c:\Users\Amir\Desktop\vending_machines\node_modules\bluebird\js\release\async.js:17:14) 在 runCallback (timers.js:672:20) 在 tryOnImmediate (timers.js:645:5) 在 processImmediate [as _immediateCallback] (timers.js:617:5)

最佳答案

当您尝试解构一个对象(因为它是数组)时,可能会出现此错误。

我敢打赌,你的错误就在这里:

  if (ValidatedAfterFind(beverage, beverageid) && 
ValidatedAfterFind(machine, machineid)) {

您应该检查那里的饮料机器

<小时/>

const destructureMe = [
'a',
'b',
];

const destructureMeToo = {};

function func([ result ]) {
console.log(result);
}

func(destructureMe);

func(destructureMeToo);

关于node.js - 类型错误 : undefined is not a function - node. js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52099957/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com