按下按钮时,以下代码中的recordUpvote函数将被调用并返回此错误:
错误:TypeError: WEBPACK_IMPORTED_MODULE_0__firebase 。b.ref不是函数
我尝试将此函数导出为函数和常量。当导出为常量时,在'var Today ='的声明上引发错误。
我在此处包括了一次getGetUsers函数,因为该函数确实起作用。
import { db } from './firebase';
import moment from 'moment';
export const onceGetUsers = () =>
db.ref('users').once('value');
export function recordUpVote(instr, user) {
var today = moment().format('MMDDYYYY');
var rootRef = db.ref('/votes/' +today+ '/' +instr+ '/');
rootRef.child('voters').orderByChild('user').equalTo(user).on("value", function(snapshot) {
console.log(snapshot.val());
if(snapshot.val()==null){
// Record the vote
db.ref('/votes/' +today+ '/' +instr+ '/voters/').push({
user: user
}).then(()=>{
db.ref('/users/'+user+ '/votes/' + today+ '/').push({
instrument: instr,
vote: 'up'
}).then(()=>{
db.ref('/votes/' +today+ '/' +instr+ '/upVotes').transaction(function(upVotes) {
return upVotes + 1
});
});
});
}
snapshot.forEach(function(data) {
if(data.key==null){
// Record the vote
db.ref('/votes/' +today+ '/' +instr+ '/voters/').push({
user: user
}).then(()=>{
db.ref('/users/'+user+ '/votes/' + today+ '/').push({
instrument: instr,
vote: 'up'
}).then(()=>{
db.ref('/votes/' +today+ '/' +instr+ '/upVotes').transaction(function(upVotes) {
return upVotes + 1
});
});
});
} else {
console.log("This user already voted on this instrument on this day. Update chart.")
}
});
});
}
使用firebase.js代码更新帖子...
import firebase from 'firebase/app';
import 'firebase/auth';
import 'firebase/database';
const prodConfig = {
};
const devConfig = {
};
const config = process.env.NODE_ENV === 'production'
? prodConfig
: devConfig;
if (!firebase.apps.length) {
firebase.initializeApp(config);
}
const db = firebase.database();
const auth = firebase.auth();
export {
db,
auth,
};
答案 0 :(得分:0)
我通过从商店中调用函数来使其工作。例如...
store.js
var today = moment().format('MMDDYYYY');
db.chartUpdate(ActiveSymbol, today, User)
db.js
export const chartUpdate = (symbol, today, user) => (
console.log(symbol, today, user)
)