GVKun编程网logo

JavaScript 函数返回 null |使用 fetch 获取请求(js return null)

1

本文将带您了解关于JavaScript函数返回null|使用fetch获取请求的新内容,同时我们还将为您解释jsreturnnull的相关知识,另外,我们还将为您提供关于char*函数返回Null、C

本文将带您了解关于JavaScript 函数返回 null |使用 fetch 获取请求的新内容,同时我们还将为您解释js return null的相关知识,另外,我们还将为您提供关于char* 函数返回 Null、CreateDC 函数返回 NULL、Dart 函数返回 null、Difference between 2>&-, 2>/dev/null, |&, &>/dev/null and >/dev/null 2>...的实用信息。

本文目录一览:

JavaScript 函数返回 null |使用 fetch 获取请求(js return null)

JavaScript 函数返回 null |使用 fetch 获取请求(js return null)

如何解决JavaScript 函数返回 null |使用 fetch 获取请求

我是 JavaScript 新手。我想创建一个函数,它接受用户名并从我的本地 api 获取数据。如果用户存在,它应该返回对象,否则返回 null。问题是它总是返回 null...请告诉我是什么问题。提前致谢 这是我的代码

isUserExist=(username)=>{
    fetch(`http://localhost:8000/customer/${username}`)
    .then(response=>response.json())
    .then(data=>{       
        if(data!==null){
            return data;      
   }
     return null;
})

char* 函数返回 Null

char* 函数返回 Null

如何解决char* 函数返回 Null

我目前正在开发一个接收和读取文本文件的垃圾邮件过滤程序。在 initializeTraining 函数中,我调用了 preprocess 函数,该函数读取给定文本文件中每一行的每个字符串。

一旦从 newDict 行开始执行 first=newDict(string,NULL); 函数,程序会返回一个错误,指出 load of null pointer of type ''char'' 行中的 while(string[i] !=''\\0'' && i<WORDLENGTH) { 处有一个 newDict {1}} 函数。

preprocess 函数似乎返回空指针,尽管它仍然从文本文件中接收传入的字符串。我在 preprocess 函数中做错了什么?

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. /*
  5. WORDLENGTH is the length of the word in the linked list named as dictionary
  6. MAILSEParaTOR is the totken to differentiate the mails that are included in one file.
  7. it is also the token to update updated in linkedlist
  8. */
  9. #define WORDLENGTH 20
  10. #define MAILSEParaTOR "@#@#@"
  11. /*
  12. define DEBUG as 0 to disable debug mode and to 1 to enable the mode.
  13. */
  14. #define DEBUG 0
  15. typedef struct dictionary dict;
  16. typedef dict* word_dict;
  17. typedef enum {false,true} bool;
  18. /*
  19. linked list,count is for the total word count and
  20. occur is the numbers of the mails that had the word
  21. */
  22. struct dictionary{
  23. char word[WORDLENGTH];
  24. int occur;
  25. int count;
  26. word_dict next;
  27. bool updated;
  28. };
  29. // if there is no matching words after searching,create a new node
  30. word_dict newDict(char *string,word_dict next){
  31. word_dict target = (word_dict)malloc(sizeof(dict));
  32. int i = 0;
  33. while(string[i] !=''\\0'' && i<WORDLENGTH) {
  34. target->word[i] = string[i];
  35. i++;
  36. }
  37. target->count = 1;
  38. target->next = next;
  39. target->occur = 1;
  40. target->updated = true;
  41. return target;
  42. }
  43. /*
  44. preprocessor,convert string to lowercase
  45. and trim the puctuations at the back
  46. */
  47. char* preprocess(char* string){
  48. #if DEBUG
  49. printf("\\nbefore preprocess,string: %s \\n",string);
  50. #endif
  51. int i=0;
  52. while(string[i] != ''\\0'') { // convert to lower case
  53. if (string[i] >= 65 && string[i] < 90) {
  54. string[i] += 32;
  55. i++;
  56. }
  57. while(true) {
  58. i--;
  59. if(i < 0) {
  60. #if DEBUG
  61. printf("word of only punctuations \\n");
  62. #endif
  63. return NULL;
  64. } else if((string[i] >= 97 && string[i] <= 122) || (string[i] >= 48 && string[i] <= 57)){
  65. string[i+1]=''\\0'';
  66. break;
  67. }
  68. }
  69. i=0;
  70. while(true) {
  71. if ((string[i] >= 97 && string[i] <= 122) || (string[i] >= 48 && string[i] <= 57)){
  72. break;
  73. } else {
  74. string = &string[i+1];
  75. }
  76. i++;
  77. }
  78. }
  79. #if DEBUG
  80. printf("_after preprocess,string: %s\\n",string);
  81. #endif
  82. return string;
  83. }
  84. /*
  85. initialize training
  86. reads the sample mails and creates a linked list of
  87. the percentages of the words occuring in the sample mails
  88. */
  89. word_dict initializeTraining(char* filename){
  90. FILE *fp = NULL;
  91. fp = fopen(filename,"r");
  92. if(fp == NULL) {
  93. printf("no file found\\n");
  94. return NULL;
  95. }
  96. char* string;
  97. string = (char*)malloc(sizeof(char)*50);
  98. word_dict first = NULL;
  99. fscanf(fp,"%s\\n",string);
  100. string = preprocess(string);
  101. first = newDict(string,NULL);
  102. while(fscanf(fp,"%s",string) == 1) {
  103. first = searchDict(string,first);
  104. }
  105. fclose(fp);
  106. free(string);
  107. return first;
  108. }
  109. /*
  110. tests whether the mail is pam or not
  111. takes the filename of the test mail,returns true or false depending on the email''s content
  112. */
  113. bool bayesian_spam_filter(char * filename_for_test_email) {
  114. word_dict spamDict=initializeTraining("spam.txt");
  115. word_dict nonspamDict=initializeTraining("not_spam.txt");
  116. #if DEBUG
  117. printDict(spamDict);
  118. printDict(nonspamDict);
  119. #endif
  120. FILE *stream=NULL;
  121. stream = fopen(filename_for_test_email,"r");
  122. if(stream == NULL){
  123. printf("no file found\\n");
  124. return false;
  125. }
  126. char* string;
  127. string = (char*)malloc(sizeof(char)*50);
  128. int ps,pn; // probability of spam mail and non-spam mail
  129. double prob = 0.5;
  130. while(fscanf(stream,string) == 1){
  131. char* tempString; // for handling the errors happening from string being null during preprocessing
  132. tempString = preprocess(string);
  133. if(tempString == NULL){
  134. continue;
  135. }
  136. if((ps = searchTest(tempString,spamDict)) != 0) {
  137. if((pn = searchTest(tempString,nonspamDict)) != 0) {
  138. printf("ps:%3d,pn:%3d,%s\\n",ps,pn,tempString);
  139. prob = prob * (double) ps / ((prob* (double)ps + (1 - prob) * (double) pn));
  140. printf("this prob: %.10f\\n",prob);
  141. }
  142. }
  143. }
  144. //printf("%d,%d \\n",pSProduct,pNProduct);
  145. //proba=(float)(pSProduct/(pSProduct+pNProduct));
  146. printf("Probability of mail being spam: %.10f\\n",prob);
  147. fclose(stream);
  148. free(string);
  149. if (prob > 0.9) {
  150. return true;
  151. }
  152. return false;
  153. }

解决方法

似乎预处理函数返回空指针

当它包含一行 return NULL; 时并不奇怪。此时,您应该将 string 的第一个字符设置为 ''\\0'' 并返回它,因为周围的代码希望在所有情况下都返回字符串。

本节还有一个问题:

  1. i=0;
  2. while(true) {
  3. if ((string[i] >= 97 && string[i] <= 122) || (string[i] >= 48 && string[i] <= 57)){
  4. break;
  5. } else {
  6. string = &string[i+1];
  7. }
  8. i++;
  9. }

假设字符串是 ".a" 并通过循环。由于第一个字符不是字母,我们不会中断,而是更新 string 指针,使其现在指向 "a"。然后,我们增加 i。在下一次迭代中,string[i] 是空终止符,它不是字母,所以我们继续。由于我们已经通过了字符串的数据,接下来是未定义的行为。

对此的简单解决方法是不增加 i 而是坚持使用 [0],因为您总是想从头开始删除。正确的解决方法是使用 i 但不增加 string 指针,因为您想稍后释放它 - 您必须在 {{1} 返回的指针上调用 free },因此修改指针会导致未定义的行为!不是从 malloc 返回字符串,而是从开头返回偏移量(由 i 计算),这种方式稍后释放字符串将正常工作。调用代码将如下所示:

  1. preprocess

CreateDC 函数返回 NULL

CreateDC 函数返回 NULL

如何解决CreateDC 函数返回 NULL

我对 CreateDC 函数有问题。调用 hDC 句柄后返回 NULL

这是在按下按钮后调用函数的代码片段。 m_pprint 是指向 Print 类的指针,其中定义了与设备上下文相关的函数。 它被初始化为

m_pprint = new Print


void MainMenuDlg::OnBnClickedMenuPrint()
{
    
    int nPatientID = m_patientsDlg->m_pCurrSelData->nPatientId;
    int nExamID = m_patientsDlg->m_pCurrSelData->nExamId;
    m_outPrintingManager->PrintEcgSignal(nPatientID,nExamID);
    short* sigPointer = m_outPrintingManager->GetSamplesPointer();

    m_pprint->PrinterInit();
    m_pprint->PageSetup();
    m_pprint->StartPrint();
    m_pprint->SignalAdjust(80,1000);
    m_pprint->DrawECG(sigPointer,12,9000);
    m_pprint->EndPrint();

    
}

这是Print类的头文件

    class Print
{
protected:

    HWND hwnd;
    CDC hDC;
    int start_x;
    int start_y;
    //LPCWSTR captions[12] = { L" I ",L" II",L"III",L"aVR",L" aVL ",L"aVF",L"V1",L"V2",L"V3",L"V4",L"V5",L"V6" };
    LPCWSTR captions[12];
    int limit_x;
    int limit_y;
    int printmode;
    int width;
    int new_sign_len;
    int channel_nr;
    int startSample;
    int channels[13];
    int mode;
    float sig_speed;
    float amplitude;
    int offset;
    int channel_start;
    int channel_stop;
    int tmp_channel;
    float frequency;
    int space;
    int page;
    BOOL orientation;
public:
    Print();
    ~Print();
    int Print();
    BOOL PrinterInit();
    BOOL PageSetup();
    BOOL StartPrint();
    BOOL DrawGrid(int start_x,int start_y,int limit_x,int limit_y,int width);
    BOOL EndPrint();
    void DrawECG(short * pointer,int channel_nr,int channelLength);
    void SignalAdjust(int start_x,int limit_x);
    void rysujEKG_Vertical(short * pointer,int start_x = 20,int limit = 1100,int offset = 60);
    void rysujEKG_Horizont(short * pointer,int start_x,int limit,int offset);
    void setSpeed(float s);
    void setAmplitude(float a);
    void setPrintMode(int printmod);
    void setChannelStart(int chan_start);
    void setChannelOrder(int * pointer_channel);
    void setChannelStop(int chan_stop);
    void setChannelNr(int chan_nr);
    void setSpace(int sp);
    void setPage(int pg);
    void setSignalLength(int sl);
    void setorientation(int orient);
    void setStartSample(int startsample);
    float getSpeed();
    float getAmplitude();
    float getFrequency();
    int getChannelNr();
    int getPrintMode();
    int getChannelStart();
    int getChannelStop();
    int getSpace();
    void DrawScaLevertic(int x,int y);
    void DrawExaminationTextBox_Horizont(int start_x,int stop_y);

}

;

和函数 PageSetup,其中 CreateDC 正在调用。 hDCCDC 类型。 像 CDC hDC

一样初始化
BOOL Print::PageSetup()
{

    ZeroMemory(&psd,sizeof(psd));
    psd.lStructSize = sizeof(psd); // psd is member of PAGESETUPDLG structure
    psd.hwndOwner = NULL;
    psd.hDevMode = hMode; // Don''t forget to free or store hDevMode.
    psd.hDevNames = hNames; // Don''t forget to free or store hDevNames.
    psd.Flags = PSD_INHUNDREDTHSOFMILLIMETERS | PSD_MARGINS | PSD_MINMARGINS;
    psd.rtMargin.top = user_margins.top;
    psd.rtMargin.left = user_margins.left;
    psd.rtMargin.right = user_margins.right;
    psd.rtMargin.bottom = user_margins.bottom;
    psd.rtMinMargin.top = 100;
    psd.rtMinMargin.left = 100;
    psd.rtMinMargin.right = 100;
    psd.rtMinMargin.bottom = 100;
    psd.ptPaperSize.x = papersize.x;
    psd.ptPaperSize.y = papersize.y;
    psd.lpfnPagePaintHook = NULL;
    lf_vertic.lfheight = 30; // is member of LOGFONT structure
    lf_vertic.lfWidth = 0;
    //lf.lfOrientation = 2700;
    lf_vertic.lfEscapement = 0;
    lf_vertic.lfUnderline = FALSE;
    lf_vertic.lfWeight = FW_BOLD;
    lf_horizont.lfheight = 30;
    lf_horizont.lfWidth = 0;
    //lf.lfOrientation = 2700;
    lf_horizont.lfEscapement = 2700;
    lf_horizont.lfUnderline = FALSE;
    lf_horizont.lfWeight = FW_BOLD;

    lf_exam_param.lfheight = 30;
    lf_exam_param.lfWidth = 0;
    lf_exam_param.lfEscapement = 2700;
    lf_exam_param.lfUnderline = FALSE;
    lf_exam_param.lfWeight = FW_norMAL;

    hFontVertic = CreateFontIndirect(&lf_vertic);
    hFontHorizont = CreateFontIndirect(&lf_horizont);
    hFontExamParam = CreateFontIndirect(&lf_exam_param);
    PRINTDLG pd;
    pd.hwndOwner = NULL;
    pd.rcMargin.top = 3000;


    if (PageSetupDlg(&psd) == TRUE)
    {
        memcpy(&dm,(DEVMODE *)(psd.hDevMode),sizeof(DEVMODE));
        lstrcpy(DriverName,((TCHAR *)((BYTE *)psd.hDevNames + ((DEVNAMES *)psd.hDevNames)->wDriverOffset)));
        lstrcpy(DeviceName,((TCHAR *)((BYTE *)psd.hDevNames + ((DEVNAMES *)psd.hDevNames)->wDeviceOffset)));
        lstrcpy(OutputName,((TCHAR *)((BYTE *)psd.hDevNames + ((DEVNAMES *)psd.hDevNames)->wOutputOffset)));
        lstrcpy(OutputName,((TCHAR *)((BYTE *)psd.hDevNames + ((DEVNAMES *)psd.hDevNames)->wOutputOffset)));
        user_margins.left = psd.rtMargin.left;
        user_margins.right = psd.rtMargin.right;
        user_margins.bottom = psd.rtMargin.bottom;
        user_margins.top = psd.rtMargin.top;
        papersize.x = psd.ptPaperSize.x;
        papersize.y = psd.ptPaperSize.y;
        dm.dmPrintQuality = 300;
        dm.dmPaperSize = DMPAPER_A4;
        hDC.CreateDC(DriverName,DeviceName,OutputName,&dm);
        return TRUE;
}

解决方法

当我使用 DEVMODE 结构时,我是这样做的:

void CCommunityTalksApp::GetPageSettings(CPrintInfo *pInfo,short &dmPaperSize,short &dmOrientation)
{
    PRINTDLG    pd;
    DEVMODE FAR *pDevMode = nullptr;

    pd.lStructSize = (DWORD)sizeof(PRINTDLG);

    if(pInfo == nullptr)
    {
        if(GetPrinterDeviceDefaults(&pd))
        {
            // protect memory handle with ::GlobalLock and ::GlobalUnlock
            pDevMode=(DEVMODE FAR *)::GlobalLock(m_hDevMode);
        }
    }
    else
    {
        // protect memory handle with ::GlobalLock and ::GlobalUnlock
        pDevMode=(DEVMODE FAR *)::GlobalLock(pInfo->m_pPD->m_pd.hDevMode);
    }

    if(pDevMode != nullptr)
    {
        // is it set to landscape
        dmPaperSize = pDevMode->dmPaperSize;
        dmOrientation = pDevMode->dmOrientation;
    }

    if(pInfo == nullptr)
    {
        ::GlobalUnlock(m_hDevMode);
    }
    else
    {
        ::GlobalUnlock(pInfo->m_pPD->m_pd.hDevMode);
    }
}

如您所见,我使用了 ::GlobalLock::GlobalUnlock 方法:

  • pDevMode=(DEVMODE FAR *)::GlobalLock(m_hDevMode)
  • pDevMode=(DEVMODE FAR *)::GlobalLock(pInfo->m_pPD->m_pd.hDevMode)
  • ::GlobalUnlock(m_hDevMode)
  • ::GlobalUnlock(pInfo->m_pPD->m_pd.hDevMode)

Dart 函数返回 null

Dart 函数返回 null

如何解决Dart 函数返回 null

我有一个函数调用请求在服务器中创建一个新学生

  1. createStudents(String studentName) async {
  2. await Webservice()
  3. .postReq(new API().createNewStudent(context),studentName)
  4. .then((value) {
  5. if (value is Students) {
  6. Students std = value;
  7. helper.initializeDatabase();
  8. helper.insertStudents(std);
  9. return "Sucessfull";
  10. } else {
  11. return value;
  12. }
  13. });

}

在这个例子中,我发送了学生的名字,并在服务器中创建了新学生。

如果成功则返回一个学生对象。

如果不成功则返回消息。

我在这个函数中调试并检查了值,但它总是返回 null。

成功时返回Student对象,但createStudents函数返回null。错误信息相同

  1. var ab = await createTags(data);
  2. print(ab);

ab 始终为空。

我不知道为什么它总是返回 null。

解决方法

我不是 100% 肯定,但我最好的猜测是你忘记了“return”语句

  1. createStudents(String studentName) async {
  2. //Here should be return
  3. final value = await Webservice()
  4. .postReq(new API().createNewStudent(context),studentName);
  5. if (value is Students) {
  6. Students std = value;
  7. helper.initializeDatabase();
  8. helper.insertStudents(std);
  9. return "Sucessfull";
  10. } else {
  11. return value;
  12. }
  13. }

Difference between 2>&-, 2>/dev/null, |&, &>/dev/null and >/dev/null 2>...

Difference between 2>&-, 2>/dev/null, |&, &>/dev/null and >/dev/null 2>...


http://unix.stackexchange.com/questions/70963/difference-between-2-2-dev-null-dev-null-and-dev-null-21


Just looking for the difference between

  • 2>&-
  • 2>/dev/null
  • |&
  • &>/dev/null
  • >/dev/null 2>&1

and their portability with non-Bourne shells like tcsh, mksh, etc.

For background:

  • a number 1 = standard out (i.e. STDOUT)
  • a number 2 = standard error (i.e. STDERR)
  • if a number isn''t explicitly given, then number 1 is assumed by the shell (bash)

First let''s tackle the function of these. For reference see the Advanced Bash-Scripting Guide.

Functions

2>&-

The general form of this one is M>&-, where "M" is a file descriptor number. This will close output for whichever file descriptor is referenced, i.e. "M".

2>/dev/null

The general form of this one is M>/dev/null, where "M" is a file descriptor number. This will redirect the file descriptor, "M", to /dev/null.

2>&1

The general form of this one is M>&N, where "M" & "N" are file descriptor numbers. It combines the output of file descriptors "M" and "N" into a single stream.

|&

This is just an abbreviation for 2>&1 |. It was added in Bash 4.

&>/dev/null

This is just an abbreviation for >/dev/null 2>&1. It redirects file descriptor 2 (STDERR) and descriptor 1 (STDOUT) to /dev/null.

>/dev/null

This is just an abbreviation for 1>/dev/null. It redirects file descriptor 1 (STDOUT) to /dev/null.

Portability to non-bash, tcsh, mksh, etc.

I''ve not dealt much with other shells outside of csh and tcsh. My experience with those 2 compared to bash''s redirection operators, is that bash is superior in that regard. See the tcsh man page for more details.

Of the commands you asked about none are directly supported by csh/tcsh. You''d have to use different syntaxes to construct similar functions.


关于JavaScript 函数返回 null |使用 fetch 获取请求js return null的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于char* 函数返回 Null、CreateDC 函数返回 NULL、Dart 函数返回 null、Difference between 2>&-, 2>/dev/null, |&, &>/dev/null and >/dev/null 2>...等相关知识的信息别忘了在本站进行查找喔。

本文标签: