我以前看过这个问题,但没有找到答案。我的函数定义明确,我甚至在调用它之前就放置了一个断点,并检查了它的“ 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
}
这让我非常疯狂,找不到答案!
我认为以前可能与循环引用有关,但没有类似的东西。
任何帮助将不胜感激。
谢谢。
堆栈跟踪: 未处理的拒绝TypeError:undefined不是函数 在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) 在Instant.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)
答案 0 :(得分:1)
当您尝试将对象分解为数组时会出现此错误。
我敢打赌,您的错误在这里:
if (ValidatedAfterFind(beverage, beverageid) &&
ValidatedAfterFind(machine, machineid)) {
您应该在此处检查beverage
和machine
const destructureMe = [
'a',
'b',
];
const destructureMeToo = {};
function func([ result ]) {
console.log(result);
}
func(destructureMe);
func(destructureMeToo);